我正在我的 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/