Re: Coordinate conversions in CALayer
Re: Coordinate conversions in CALayer
- Subject: Re: Coordinate conversions in CALayer
- From: Graham Cox <email@hidden>
- Date: Wed, 23 Jan 2013 17:41:13 +1100
On 22/01/2013, at 4:35 PM, Andy Lee <email@hidden> wrote:
> On Jan 21, 2013, at 5:12 PM, Graham Cox <email@hidden> wrote:
>> My question is, is there a way to directly convert coordinates between two unrelated layers in a tree, or are these methods implemented by recursion up to a common parent node and then back down to the target layer? If I had some hint of how this was done in the real CALayer architecture it would help me avoid wasting time trying various blind alleys.
>
> Unless and until someone chimes in with a more sophisticated solution, you could convert the rect from layer A to the root layer, and from the root layer to layer B, then find out if that bogs down performance.
Thanks Andy,
I'm doing something rather like this, and it works well enough. In fact I'm not sure there really is a need to translate between arbitrary layers across the tree - 99 times out of 100 I'm just transforming up or down one branch in the tree, and in practice the tree is usually shallow.
I think I need to change the focus of my question though, because before I can get to this, I need to work out precisely how I should be manipulating my transforms. As usual with transforms, what starts off looking straightforward turns into a mindbending nightmare quite quickly.
My model looks pretty similar to CALayer, so my layer objects have a bounds, frame, an anchor point, a transform and a position exactly as they are defined for CALayer.
When a layer draws its sublayer, it applies the following transformations to the context:
CGPoint anch = layer.anchorPoint;
CGRect br = layer.bounds;
CGPoint pos = layer.position;
CGContextTranslateCTM( ctx, pos.x, pos.y );
CGContextConcatCTM( ctx, layer.transform );
CGContextTranslateCTM(ctx, -(br.origin.x + anch.x * br.size.width), -(br.origin.y + anch.y * br.size.height));
That works fine.
Where I'm getting into trouble is translating a rect from the layer's local coordinates back up to super. It works until I have a transform property that does something. So I'm doing this:
CGRect br = self.bounds;
CGPoint anch = self.anchorPoint;
CGAffineTransform tfm = CGAffineTransformMakeTranslation( -self.position.x, -self.position.y );
tfm = CGAffineTransformConcat( tfm, self.transform ); //<------ this screws things up if it's not the identity matrix
return CGAffineTransformTranslate( tfm, (br.origin.x + anch.x * br.size.width), (br.origin.y + anch.y * br.size.height));
To create a transform that supposedly will transform from the superlayer TO the layer, and I then invert it to go back the other way. It works in the simple case, but a rotation transform upsets things. I've tried pretty much every combination of ordering different things in constructing the transform but this is the "best" result I get. Still wrong though. Funny, every time I think I have transforms figured out something turns up to show me I don't.
I just need someone to point out my error, then I think I'll be in business.
--Graham
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden