CALayer上的hitTest - 你如何找到被击中的实际图层?

Ada*_*dam 4 calayer ios

情况:需要找到用户触摸的图层.

问题:Apple表示我们应该使用[CALayer presentationLayer]进行命中测试,以便它代表当时屏幕上的实际内容(它捕获动画中期等信息).

...除了:presentationLayer不返回原始图层,它返回它们的副本......所以:hitTest将返回一个全新的CALayer实例,该实例与原始实例不同.

我们如何找到被击中的实际CALayer?

例如

CALayer* x = [CALayer layer];
CALayer* y = [CALayer layer];
[self.view.layer addSublayer: x];
[self.view.layer addSublayer: y];

...

CALayer* touchedLayer = [self.view.layer.presentationLayer hitTest:touchPoint];
Run Code Online (Sandbox Code Playgroud)

...但是,触摸的是"x",还是"y"?

if( touchedLayer == x ) // this won't work, because touchedLayer is - by definition from Apple - a new object
Run Code Online (Sandbox Code Playgroud)

So *_* It 7

亚当的回答是正确的,并帮助了我.这是我用过的代码,可以帮助其他人.

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    CGPoint touchPoint = [(UITouch*)[touches anyObject] locationInView:self];
    CALayer *touchedLayer = [self.layer.presentationLayer hitTest:touchPoint];  // is a copy of touchedLayer
    CALayer *actualLayer = [touchedLayer modelLayer];   // returns the actual layer
    NSLog (@"touchedLayer: %@", touchedLayer);
    NSLog (@"actualLayer: %@", actualLayer);
}
Run Code Online (Sandbox Code Playgroud)


Ada*_*dam 4

啊! 刚刚想通了这一点,阅读了有关 CALayer 另一个问题的邮件列表帖子。

调用 [CALayerpresentationLayer] 并使用图层树的“演示克隆”后​​,您可以获取该树中的任何对象,并在其上调用 [CALayermodelLayer]以在相同位置取回原始参考对象原来的树

该参考是稳定的(经过测试 - 它有效)。

苹果的文档在这一点上有点……晦涩难懂。他们暗示它“有时”会失败(“ ......结果未定义”) - 但现在对我来说已经足够了。