Ios 快速检测绘图中的触碰位置:

Ios 快速检测绘图中的触碰位置:,ios,swift,xcode,Ios,Swift,Xcode,提前道歉这是我第一次发布问题,我是这门语言的初学者,但已经从论坛和文档中删除了 要创建的图形类型,请单击以查看 如果我要创建一个类似于所附照片的图形,是否有一种方法可以根据点击的片段执行函数 有没有办法向draw中创建的形状添加发件人标记, 比如 本质上,我可以创建闭合形状,并通过使用某种可以识别的标记来确定它们何时被点击 非常感谢您提前提供的任何帮助 UIBezierPath具有“contains”方法 因此,在touchesbented或touchesend中使用此方法保存您的形状并测试

提前道歉这是我第一次发布问题,我是这门语言的初学者,但已经从论坛和文档中删除了

要创建的图形类型,请单击以查看

如果我要创建一个类似于所附照片的图形,是否有一种方法可以根据点击的片段执行函数

有没有办法向draw中创建的形状添加发件人标记, 比如

本质上,我可以创建闭合形状,并通过使用某种可以识别的标记来确定它们何时被点击


非常感谢您提前提供的任何帮助

UIBezierPath具有“contains”方法

因此,在touchesbented或touchesend中使用此方法保存您的形状并测试命中率

class   SomeV: UIView {
    var shape1 = UIBezierPath()
    var shape2 = UIBezierPath()
    var shape3 = UIBezierPath()
    override func draw(_ rect: CGRect) {
        shape1 = UIBezierPath() // code for segment 1
        shape2 = UIBezierPath() // code for segment 2
        shape3 = UIBezierPath() // code for segment 3
        // etc
    }
    override func
    touchesEnded( _ touches: Set<UITouch>, with event: UIEvent? ) {
        if let w = touches.first?.location( in: self ) {
            if shape1.contains( w ) { print( "Touches in shape1" ) }
            if shape2.contains( w ) { print( "Touches in shape1" ) }
            if shape3.contains( w ) { print( "Touches in shape1" ) }
        }
    }
}
类SomeV:UIView{
var shape1=UIBezierPath()
var shape2=UIBezierPath()
var shape3=UIBezierPath()
重写函数绘图(rect:CGRect){
shape1=UIBezierPath()//段1的代码
shape2=UIBezierPath()//段2的代码
shape3=UIBezierPath()//段3的代码
//等
}
覆盖函数
touchesend(utouchs:Set,带有事件:UIEvent?){
如果让w=接触.first?位置(in:self){
如果shape1.包含(w){print(“在shape1中接触”)}
如果shape2.包含(w){print(“在shape1中接触”)}
如果shape3.包含(w){print(“在shape1中接触”)}
}
}
}

太棒了,非常感谢您的回答和快速响应!
class   SomeV: UIView {
    var shape1 = UIBezierPath()
    var shape2 = UIBezierPath()
    var shape3 = UIBezierPath()
    override func draw(_ rect: CGRect) {
        shape1 = UIBezierPath() // code for segment 1
        shape2 = UIBezierPath() // code for segment 2
        shape3 = UIBezierPath() // code for segment 3
        // etc
    }
    override func
    touchesEnded( _ touches: Set<UITouch>, with event: UIEvent? ) {
        if let w = touches.first?.location( in: self ) {
            if shape1.contains( w ) { print( "Touches in shape1" ) }
            if shape2.contains( w ) { print( "Touches in shape1" ) }
            if shape3.contains( w ) { print( "Touches in shape1" ) }
        }
    }
}