在 CALayer 上使用 hitTest - 你如何找到哪个层被击中 (Swift)?
Using hitTest on a CALayer - how do you find which layer was hit (Swift)?
你如何找到被击中的实际层?
目前我有很多 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
}
}
}
你如何找到被击中的实际层?
目前我有很多 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
}
}
}