Re: Animating autolayout constraint changes for subviews
Re: Animating autolayout constraint changes for subviews
- Subject: Re: Animating autolayout constraint changes for subviews
- From: David Duncan <email@hidden>
- Date: Fri, 30 Dec 2016 11:38:04 -0800
> On Dec 28, 2016, at 4:14 PM, Doug Hill <email@hidden> wrote:
>
> Hi Ken,
>
> I uploaded a sample project here:
>
> https://github.com/djfitz/TestAutolayoutAnimations
>
> I tried to strip this down to what is needed to show the behavior I see.
>
> My code to actually do the animation is this:
>
> - (IBAction)animateIt:(id)sender
> {
> static BOOL small = NO;
>
> if( small )
> {
> [self.view layoutIfNeeded];
>
> self.containerWidthConstraint.constant = self.view.frame.size.width;
>
> [UIView animateWithDuration:1 animations:
> ^{
> [self.view layoutIfNeeded];
> }];
> }
> else
> {
> [self.view layoutIfNeeded];
>
> self.containerWidthConstraint.constant = self.view.frame.size.width / 2;
>
> [UIView animateWithDuration:1 animations:
> ^{
> [self.view layoutIfNeeded];
> }];
> }
>
> small = !small;
> }
>
> 'container view' has one subview which is a UILabel. The label is pinned to the superview edges via autolayout constraints. (e.g. trailing, leading, top, bottom edges all pinned to superview edges.)
>
> I tried a few different variations, including leaving out the first layoutIfNeeded (which some people say should be done, others not).
>
> The exact behavior is that the label will resize to the new size immediately and reflow the text, then the container view will animate it's size change. It would be nice if both the label and the container view animate at the same time.
> Also, as I mentioned, a button will exhibit the same behavior, probably because it has a UILabel inside it to show the button text.
Labels (and other content provided via -drawRect:) will almost universally behave this way without additional work on your part, as the content is redrawn instantly at the final size. At best you might get an animation where the content resizes into place, but more often than not, it will pop in some undesirable way. Generally the only way to get good animations with drawn content is to snapshot in some way and execute an custom animation from the old to the new content (snapshotting isn’t necessarily literal here – it can just mean creating a new label with the new content as one example).
>
> Thanks again for any ideas.
>
> Doug Hill
>
>
>> On Dec 28, 2016, at 12:50 PM, Ken Thomases <email@hidden> wrote:
>>
>> On Dec 28, 2016, at 1:55 PM, Doug Hill <email@hidden> wrote:
>>>
>>> I can now animate my constraint changes but I notice that subviews aren't animated. For example, I have a single view with a width constraint, and this view has a label as a subview that expands to the size of it's parent view via edge constraints.
>>> I can change the width constraint constant of the parent view at runtime and it animates very well. However, the subviews jump into place immediately then the parent view animates into place. I see the same behavior with a button as a subview.
>>
>> Show exactly how you're animating the constraint changes. Are you really animating the change of the constraint or are you doing a layoutIfNeeded within an animation context? Even if the former, are you calling any methods that force layout (layoutIfNeeded or similar)? If so, where/when?
>>
>> Regards,
>> Ken
>>
>
>
> _______________________________________________
>
> 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
--
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