Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert a CGPoint from a UIView coordinate system to a CALayer coordinate system

There are methods to transfer a CGPoint from one UIView to another and from one CALayer to another. I cannot find a way to change a CGPoint from a UIView's coordinate system to a CALayer's coordinate system.

Does a layer and it's host view have the same coordinate system? Do I need to transform points between them?

Thanks, Corey

[EDIT]

Thanks for the answer! It is hard to find information on the differences/similarities between CA on the iPhone and Mac. I am surprised I couldn't find this issue addressed directly in any Apple Documentation.

I was pursuing this answer to help with a bug I am troubleshooting, and this was so far my best guess, but I suppose I am barking up the wrong tree. If the coordinate systems are the same, then I have another issue...

The actual issue I am having can be found here on Stack Overflow: layer hit test only returning layer when bottom half of layer is touched

like image 441
Corey Floyd Avatar asked Jan 04 '09 01:01

Corey Floyd


2 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];
like image 170
schwa Avatar answered Sep 29 '22 04:09

schwa


In terms of Apple documentation, I found an "iPhone OS Note" in the Layer Coordinate System section of the Core Animation Programming Guide (2008-11-13).

The default root layer of a UIView instance uses a flipped coordinate system that matches the default coordinate system of a UIView instance–the origin is in the top-left and values increase down and to the right. Layers created by instantiating CALayer directly use the standard Core Animation coordinate system.

like image 27
otto Avatar answered Sep 29 '22 04:09

otto