gpt4 book ai didi

ios - 在 CALayer 上使用 hitTest - 你如何找到哪个层被击中 (Swift)?

转载 作者:可可西里 更新时间:2023-11-01 01:06:05 24 4
gpt4 key购买 nike

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

目前我有很多 CAShapeLayers,我希望每个人在被点击时都做一些事情。但是,当它们中的任何一个被点击时,它们都会发生一个 Action 。

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

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

}

关于ios - 在 CALayer 上使用 hitTest - 你如何找到哪个层被击中 (Swift)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29788663/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com