Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

hitTest on CALayer - how do you find which actual layer was hit?

Tags:

ios

calayer

Situation: need to find which layer the user has touched.

Problem: Apple says we should use [CALayer presentationLayer] to do hit-testing, so that it represents what is actually on screen at the time (it captures info mid-animation etc).

...except: presentationLayer does NOT return the original layers, it returns copies of them ... so: the hitTest will return a brand new CALayer instance that is not equivalent to the original.

How do we find which actual CALayer was hit?

e.g.

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];

...but, is touchedLayer "x", or is it "y"?

if( touchedLayer == x ) // this won't work, because touchedLayer is - by definition from Apple - a new object
like image 611
Adam Avatar asked Nov 24 '11 15:11

Adam


2 Answers

Adam's answer is correct and helped me out. Here's the code that I used that may help someone else out.

- (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);
}
like image 164
So Over It Avatar answered Oct 13 '22 14:10

So Over It


Ah! Just figured this out, reading a mailing list post on a different problem with CALayer.

After calling [CALayer presentationLayer], and working with the "presentation clone" of the layer-tree, you can take any object in that tree, and call [CALayer modelLayer] on it to get back the original reference object at the same position in the original tree.

This reference is stable (tested - it works).

Apple's docs are a bit ... obscure ... on this one. And they imply it will "sometimes" fail ("...results are undefined") - but it's good enough for me for now.

like image 4
Adam Avatar answered Oct 13 '22 15:10

Adam