2008 05 06Core Animation's convertPoint:toLayer: and sublayerTransform
Quick note : if you have a container
CALayer
containing layers arranged in a grid, have a sublayerTransform
setup on it, and want to get its transformed layer coordinates on a mouseDown
, you'll need a helper layer :
// We're in mouseDown
CGPoint point = NSPointToCGPoint([self convertPoint:event.locationInWindow fromView:nil]);
// Build a dummy CALayer and insert it
CALayer* helperLayer = [CALayer layer];
[containerLayer addSublayer:helperLayer];
// Get coordinates
CGPoint layerPoint = [[self layer] convertPoint:point toLayer:helperLayer];
// Remove our helper layer
[helperLayer removeFromSuperlayer];
Now why can't we just ask for containerLayer
's coordinates ? Because it has a sublayerTransform
, so its coordinates are really the same as its parent. We can't ask for one of containerLayer.sublayers
either : being laid out (layouted ? :) ) in a grid, they each have their own coordinate system. We therefore use a dummy layer : it has a default position of [0, 0] and will give the point transformed trough sublayerTransform
.