在CALayer上使用hitTest - 如何找到被击中的图层(Swift)?

时间:2015-04-22 05:42:20

标签: ios swift cashapelayer

如何找到被击中的实际图层?

目前我有很多CAShapeLayers,我想让每个人在点击时做点什么。但当他们中的任何一个被窃听时,他们所有人都会采取行动。

到目前为止,这是我的代码:

    if shape.hitTest(point) != nil {
        shape.lineWidth = 60
    }
    if shape1.hitTest(point) != nil {
        shape1.lineWidth = 60
    }
    if shape2.hitTest(point) != nil {
        shape2.lineWidth = 60
    }

这可能意味着它只是检查点击的点是否为CAShapeLayer。如何检查点击的点是否为特定CAShapeLayer

1 个答案:

答案 0 :(得分:3)

这应该有效:

override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {

    // Declare the touched symbol and its location on the screen
    for touch: AnyObject in touches {

        //get the point location
        let location:CGPoint = (touch as! UITouch).locationInView(self.view)

        if CGPathContainsPoint(shape1.path, nil, location, false) //shape 1
        {
            //touch in shape 1
        }
        else if CGPathContainsPoint(shape2.path, nil, location, false) //shape 2
        {
            //touch in shape 2
        }
        else if CGPathContainsPoint(shape3.path, nil, location, false) //shape 3
        {
            //touch in shape 3
        }
    }

}