Re: Confusing CALayer Transform/Frame/Bounds Behavior
Re: Confusing CALayer Transform/Frame/Bounds Behavior
- Subject: Re: Confusing CALayer Transform/Frame/Bounds Behavior
- From: David Duncan <email@hidden>
- Date: Wed, 25 Jan 2012 13:15:18 -0800
On Jan 25, 2012, at 11:24 AM, Seth Willits wrote:
> On Jan 24, 2012, at 5:52 PM, David Duncan wrote:
>
>>> Can anyone explain what's going on?
>>
>>
>> The frame is an entirely synthesized property, based on the bounds, position, anchorPoint and transform of a given layer. When you set the latter parameters, the frame updates automatically but when you set the frame Core Animation has to derive the new bounds & position values by applying the anchorPoint and inverse transform. Thus when you set the frame of a layer while it is under a transform, the inverse transform is first applied to get a new size, then that size updates the bounds size.
>
> Short of guessing the order of operations and doing the matrix math myself, I have only intuition to tell me that the size ending up at {1000, 0} is… odd. I'm hesitant to say wrong, but in my head I don't see who the inverse x the frame = {1000, 0}.
Its possible that there is a bug here, but from a practical perspective, setting the frame while under the presence of a transform can cause many unexpected results, more so than just this one.
>> Moral of the story is that when a layer is transformed (or may be) you should only manipulate its position and bounds. Stay far far away from the frame property, as its just a bag of hurt.
>
> So let me just throw this out there. Is there some reason it couldn't be changed so that
>
> layer.frame = (0, 0, 100, 100);
> layer.transform = xfm;
> layer.frame = (0, 0, 200, 200);
>
> is equivalent to:
>
> layer.frame = (0, 0, 100, 100);
> layer.transform = xfm;
> layer.transform = identity;
> layer.frame = (0, 0, 200, 200);
> layer.transform = xfm;
>
> The latter behaves nice and predictably, and if everyone is avoiding the "bag full of hurt" does it actually break anything? *wishful thinking*
What you need to remember is that the frame is a fiction.
Lets pretend that things work as you want them to, that when you set the layer's frame there isn't a matrix inverse applied. Since the frame does not really exist, that means that you need to set the bounds and position as if the transform were identity. So now the bounds and position have been set, but then to determine the frame you need to apply the transform and anchorPoint to get them back. You now have a frame that is not the same as what you set it to.
The way things work currently is consistent (modulo bugs of course) if not obvious. But it is for that very reason why when a layer has been transformed, you should set aside the technique of setting the frame, because you are not necessarily setting what you think you are when you do so under those conditions.
--
David Duncan
_______________________________________________
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