Re: Layout-triggered animation
Re: Layout-triggered animation
- Subject: Re: Layout-triggered animation
- From: Jakob Egger <email@hidden>
- Date: Fri, 18 Apr 2014 13:25:29 +0200
Hi Julian,
The jittering is probably a result of misusing the animator proxy! The animator proxy is used when you want to directly change properties, for example the frame of a view, or the alpha value. When you use autolayout, the frame will be set by the view's layout method, which does not use the animator. Therefore you must capture the change using setAllowsImplicitAnimation.
When you perform an animation in autolayout, make sure that the views are layed out before the animation starts. You must call -layoutSubtreeIfNeeded twice.
Try the following:
-(void) expand {
[view.superview layoutSubtreeIfNeeded];
[NSAnimationContext beginGrouping];
[[NSAnimationContext currentContext] setAllowsImplicitAnimation: YES];
[[NSAnimationContext currentContext] setDuration:2];
[NSAnimationContext currentContext].timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
[view removeConstraint:collapseConstraint];
[view.superview layoutSubtreeIfNeeded];
[NSAnimationContext endGrouping];
}
See also the section "Animating Changes Made by Auto Layout" on the following page, which has a better example:
https://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/AutolayoutPG/AutoLayoutbyExample/AutoLayoutbyExample.html
Furthermore, adding/removing constraints in autolayout is an expensive operation. The only cheap operation is to change the constant of a constraint. Maybe it would be possible to do this in your case? Since the constraint you are removing limits the height, maybe just setting the constraint's constant to a high value would be faster?
Best wishes,
Jakob
Am 14.04.2014 um 23:42 schrieb Julian <email@hidden>:
> I'm animating frame changes from autolayout. Code to trigger the expand is
> shown here. I added QuartzCore.framework to my link libraries, and checked
> layer backing for the window's view.
>
> -(void) expand {
> [NSAnimationContext beginGrouping];
> [[NSAnimationContext currentContext] setAllowsImplicitAnimation: YES];
> [[NSAnimationContext currentContext] setDuration:2];
> [NSAnimationContext currentContext].timingFunction =
> [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
>
> [[view animator] removeConstraint:collapseConstraint];
>
> [[view animator].superview layoutSubtreeIfNeeded];
> [NSAnimationContext endGrouping];
> }
>
> The collapseConstraint is a constraint that limits the view to a short
> height, however there is another constraint that establishes the height at
> its natural height, with a lower priority. collapseConstraint trumps it,
> until it's removed, then boom! layout expands it and moves down the views
> below.
>
> 2 questions:
>
> 1. How do I avoid using -setAllowsImplicitAnimation:? From the
> documentation, it sounded like using the animator proxy automatically
> handled implicit animations, yet if I remove that line, there are no
> animations.
>
> 2. I added a recording of this expand animation, and the collapse
> animation, on youtube here. http://youtu.be/fSu0EqE9mnM
>
> There is significant vertical jitter in the animation of the expanding
> view. For example, the horizontal line, which is an NSBox subview at the
> top edge of the expanding view, flickers in/out of visibility. The other
> lines inside the views that are getting shifted down don't have this
> problem. What might cause this? Perhaps some views inside are changing
> height as the frame changes?
_______________________________________________
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