ios - 检测描边 uibezierpath 包含点

标签 ios swift mobile uibezierpath

我正在我的 View 中绘制一条描边路径。我正在尝试查看描边路径是否包含特定点。但 contains 方法不会检测该点是否位于描边路径中。

func checkCollision(currentPoint:CGPoint) -> CustomShape?{
    for shape in customShapes {
        //Check if current shapes uibezierpath contains a point
        if (shape.path.contains(currentPoint)) {
            return shape
        }
    }
    return nil
}

最佳答案

swift 5.x

您可以使用此扩展程序来快速获得答案:

extension UIBezierPath {
    func isInsideBorder(_ pos:CGPoint, tolleranceWidth:CGFloat = 2.0)->Bool{
        let pathRef = cgPath.copy(strokingWithWidth: tolleranceWidth, lineCap: CGLineCap.butt, lineJoin: CGLineJoin.round, miterLimit: 0)
        let pathRefMutable = pathRef.mutableCopy()
        if let p = pathRefMutable {
            p.closeSubpath()
            return p.contains(pos)
        }
        return false
    }
}
<小时/>

用法:

var currentPoint:CGPoint= CGPoint.zero
if yourBeizerPath.isInsideBorder(currentPoint, tolleranceWidth:3.0) {
   /// Let's go, it's inside..
}

关于ios - 检测描边 uibezierpath 包含点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48759013/

相关文章:

ios - 从 swift 2.3 问题迁移到 swift 3.1

iphone - 如何将 iOS 无线 (OTA) 版本同步回 iTunes?

ios - 如何创建开发/暂存/生产环境

没有 Storyboard或图像的 iOS 启动图像

javascript - 检测移动设备

java-me - J2ME背光

ios - 获取细胞索引-偷看和流行

iphone - NSDate到NSString的转换提供了不同的输出

swift - SCNNode 中的 Orientation 和 Rotation 有什么区别

android - 用于制作移动应用程序界面草图的工具