Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

layer hit test only returning layer when bottom half of layer is touched

I have a sublayer on a layer-backed view. The sublayer's contents are set to an Image Ref and is a 25x25 rect.
I perform a hit test on the super layer when the touchesBegan and touchesMoved methods are invoked. The hit test method does, in fact, return the sublayer if it is touched, BUT only if the bottom half of the image is touched. If the top half of the image is touched, it returns the superlayer instead.

I do know the iPhone OS compensates for the tendancy of user touches to be lower than intended. Even if I resize the sublayer to a larger size, 50x50, it exhibits the same behavior.

Any thoughts?

like image 486
Corey Floyd Avatar asked Dec 17 '22 09:12

Corey Floyd


1 Answers

The documentation for hitTest says:

/* Returns the farthest descendant of the layer containing point 'p'.
 * Siblings are searched in top-to-bottom order. 'p' is in the
 * coordinate system of the receiver's superlayer. */

So you need to do (something like this):

CGPoint thePoint = [touch locationInView:self];
thePoint = [self.layer convertPoint:thePoint toLayer:self.layer.superlayer];
CALayer *theLayer = [self.layer hitTest:thePoint];

(Repeating answer from other post for completeness' sake)

like image 161
schwa Avatar answered Jan 01 '23 15:01

schwa