快速检测绘制中触摸的位置:

时间:2018-07-11 02:30:00

标签: ios swift xcode

道歉,这是我第一次发布问题,我是该语言的初学者,但已经抓取了论坛和文档;

我要创建的图纸类型,请单击查看

enter image description here

如果我要创建与所附照片相似的图形,是否可以根据所点击的细分执行函数。

是否可以将发件人标签添加到在绘图中创建的形状中, 如

// In custom drawing class
override func draw(_ rect: CGRect) {
    let shape1 = UIBezierPath() // code for segment 1
    let shape2 = UIBezierPath() // code for segment 2
    let shape3 = UIBezierPath() // code for segment 3
    // etc, etc
    shape1.tag = 1
    shape2.tag = 2
    shape3.tag = 3
    // etc
}

// In ViewController.swift
@IBAction func shapeButtonFunction(_ shape: Any) {
    if shape.tag == 0 {
        // do this
    }
    else if shpae.tag == 1 {
        // do this
    }
}

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

非常感谢您提前提供的帮助!

1 个答案:

答案 0 :(得分:0)

UIBezierPath具有“包含”方法。

func contains(_ point: CGPoint) -> Bool

因此,请在touchesBegan或touchesEnded内部使用此方法保存形状并测试命中率。

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" ) }
        }
    }
}