ios - 如何检测 UIBezierPath 的交集并将其删除

标签 ios swift uikit uibezierpath

我正在制作一个 iOS 绘图应用程序,并希望添加删除 UIBezierPath 将删除整个路径而不仅仅是用白色覆盖该段的功能。

我存储了一个 UIBezierPaths 数组,但我不确定为每个触摸点循环遍历整个数组、检测它是否与当前触摸点相交并删除它的效率如何它来自数组。

有什么建议吗?

最佳答案

如果我理解正确,你至少需要遍历路径。毕竟他们是独立的。

UIBezierPaths 有方法 contains 可以告诉你一个点是否在路径内。如果那是可用的,那么您需要做的就是

paths = paths.filter { !$0.contains(touchPoint) }

但由于这是绘图应用程序,因此可以安全地假设您使用的是描边而不是填充,这很可能无法按照您希望的方式使用 contains

您可以手动进行交集,在点数过多之前,它应该具有相对较好的性能。但是当你有太多点时,我打赌绘图性能会更受你的关注。包含一些笔划线宽度的交叉点仍然可能有点问题;用户需要穿过你的线的中心才能检测到命中。

有一种方法可以将描边路径转换为填充路径。这样做将使您能够使用 contains 方法。该方法称为 replacePathWithStrokedPath 但问题是它仅存在于 CGContext 上(至少我从未设法在 UIBezierPath 上找到等效项) .因此,这样做的过程包括创建上下文。例如这样的事情会起作用:

static func convertStrokePathToFillPath(_ path: UIBezierPath) throws -> UIBezierPath {
    UIGraphicsBeginImageContextWithOptions(CGSize(width: 1.0, height: 1.0), true, 1.0)
    guard let context = UIGraphicsGetCurrentContext() else { throw NSError(domain: "convertStrokePathToFillPath", code: 500, userInfo: ["dev_message":"Could not generate image context"]) }

    context.addPath(path.cgPath)
    context.setLineWidth(path.lineWidth)
    // TODO: apply all possible settings from path to context
    context.replacePathWithStrokedPath()

    guard let returnedPath = context.path else { throw NSError(domain: "convertStrokePathToFillPath", code: 500, userInfo: ["dev_message":"Could not get path from context"]) }
    UIGraphicsEndImageContext()

    return UIBezierPath(cgPath: returnedPath)
}

所以结果应该是这样的:

static func filterPaths(_ paths: [UIBezierPath], containingPoint point: CGPoint) -> [UIBezierPath] {
    return paths.filter { !(try! convertStrokePathToFillPath($0).contains(point)) }
}

关于ios - 如何检测 UIBezierPath 的交集并将其删除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56355035/

相关文章:

ios - 转换在 drawRect 中绘制的内容

ios - swift 中的排名数字循环

iphone - 打印应用程序当前堆栈跟踪的简单方法?

cocoa-touch - 如何以编程方式设置 UILabel 的背景颜色?

ios - 捕获屏幕并保存到视频 ios 时收到内存警告

ios - 更改动态字符串中的子字符串颜色

ios Iphone X 仅在从底部拖动时显示主页指示器

swift - 无法将类型 'UIViewController' 的值分配给类型 'UITabBarController?'

ios - UITextView 根据 UITableViewCell 中的文本动态宽度

ios - 在 iOS 上使用排队和 Grand Central Dispatch 的一些帮助