Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/106.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何检测UIBezierPath的交点并将其删除_Ios_Swift_Uikit_Uibezierpath - Fatal编程技术网

Ios 如何检测UIBezierPath的交点并将其删除

Ios 如何检测UIBezierPath的交点并将其删除,ios,swift,uikit,uibezierpath,Ios,Swift,Uikit,Uibezierpath,我正在制作一个iOS绘图应用程序,并希望添加一个功能,其中擦除UIBezierPath将擦除整个路径,而不仅仅是用白色覆盖线段 我存储了一个UIBezierPaths数组,但我不确定在每个接触点的整个数组中循环,检测它是否与当前接触点相交,并将其从数组中删除的效率有多高 有什么建议吗?如果我理解正确,您至少需要在路径中循环。他们毕竟是独立的 UIBezierPaths具有方法包含,该方法将告诉您某个点是否在路径内。如果这是可用的,那么您需要做的只是 paths = paths.filter {

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

我存储了一个
UIBezierPaths
数组,但我不确定在每个接触点的整个数组中循环,检测它是否与当前接触点相交,并将其从数组中删除的效率有多高


有什么建议吗?

如果我理解正确,您至少需要在路径中循环。他们毕竟是独立的

UIBezierPaths
具有方法
包含
,该方法将告诉您某个点是否在路径内。如果这是可用的,那么您需要做的只是

paths = paths.filter { !$0.contains(touchPoint) }
但由于这是绘图应用程序,因此可以安全地假设您使用的是笔划而不是填充,这很可能无法按您希望的方式使用
包含的内容

您可以手动进行交叉,直到您有太多的点为止,它应该具有相对良好的性能。但是当你有太多的分数时,我打赌绘画的表现会更受你的关注。不过,交叉点可能有点问题,包括一些笔划线宽;用户需要穿过您的线的中心来检测命中

不过,有一种方法可以将笔划路径转换为填充路径。这样做将使您能够使用
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)) }
}

要在不经过上下文的情况下将笔划路径转换为填充路径,请参见
CGPath.copy(strokingWithWidth:…)
。(这是一种基于底层CGPath的方法,而不是更高级别的UIBezierPath。)@RobNapier谢谢你。我已经找了好几年了,但一直没找到。我根本不认为它在
CGPath
上。从objectiveC开始,函数为
CGPathCreateCopyByStrokingPath
。。。想象一下我在寻找它时的沮丧。最后,我只是认为它一定是私有API的一部分。