您如何找到被击中的实际图层?
目前,我有很多人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
?
这应该工作:
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
}
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句