Re: Coordinate conversions in CALayer
Re: Coordinate conversions in CALayer
- Subject: Re: Coordinate conversions in CALayer
- From: Graham Cox <email@hidden>
- Date: Fri, 25 Jan 2013 10:12:41 +1100
On 25/01/2013, at 12:42 AM, Matt Neuburg <email@hidden> wrote:
> They are not equivalent because the first one starts with CGContextTranslateCTM but the second one starts with CGAffineTransformMakeTranslation. In other words, the first one begins by translating the current transform. The second one begins with a plain vanilla translation. The affine transform equivalent of CGContextTranslateCTM is CGAffineTransformTranslate. You might want to say this:
>
> CGAffineTransform tfm = CGAffineTransformTranslate( layer.transform, pos.x, pos.y );
Thanks for chipping in Matt, but unfortunately you're wrong ;-) Your reasoning seems sound but that's how transforms trip you up - they are rarely intuitive and seemingly are hard to tame.
The correct code is:
CGRect br = self.bounds;
CGPoint anch = self.anchorPoint;
CGAffineTransform tfm = CGAffineTransformMakeTranslation( self.position.x, self.position.y );
tfm = CGAffineTransformConcat( self.transform, tfm ); //<----- take care that tfm is the second parameter
return CGAffineTransformTranslate( tfm, -(br.origin.x + anch.x * br.size.width), -(br.origin.y + anch.y * br.size.height));
On the face of it this even seems equivalent to your suggestion, but I can tell you the result is not the same.
I have eventually stumbled upon the right result which correctly transforms down through a series of nested layers and back up again. Don't ask me how it works or I'll start to whimper.
--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