Re: Coordinate conversions in CALayer
Re: Coordinate conversions in CALayer
- Subject: Re: Coordinate conversions in CALayer
- From: Andy Lee <email@hidden>
- Date: Tue, 22 Jan 2013 00:35:25 -0500
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.
Say you already have:
- (CGRect)convertRectFromSuperlayer:(CGRect)superRect;
- (CGRect)convertRectToSuperlayer:(CGRect)localRect;
Then you could have:
// Composed in Mail without regard for efficiency.
- (CGRect)convertRectToRootLayer:(CGRect)localRect
{
CGRect result = localRect;
MyLayer *layer = self;
while ([layer superlayer])
{
result = [layer convertRectToSuperlayer:result];
layer = [layer superlayer];
}
return result;
}
// Recursion.
- (CGRect)convertRectFromRootLayer:(CGRect)rootRect
{
if ([self superlayer] == nil)
{
return rootRect;
}
CGRect superRect = [[self superlayer] convertRectFromRootLayer:rootRect];
return [self convertRectFromSuperlayer:superRect];
}
// Assumes otherLayer has same root layer.
- (CGRect)convertRect:(CGRect)localRect toLayer:(MyLayer *)otherLayer
{
CGRect rootRect = [self convertToRootLayer:localRect];
return [otherLayer convertFromRootLayer:rootRect];
}
--Andy
_______________________________________________
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