How to animate with Auto Layout

suggest change

Without Auto Layout, animation is accomplished changing a view’s frame over time. With Auto Layout, the constraints dictate the view frame, so you have to animate the constraints instead. This indirection makes animation harder to visualize.

Here are the ways to animate with Auto Layout:

  1. Change the constant of the constraint after creation using periodic calls (CADisplayLink, dispatch_source_t, dispatch_after, NSTimer). Then call layoutIfNeeded to update the constraint. Example:

Objective-C:

self.someConstraint.constant = 10.0;
[UIView animateWithDuration:0.25 animations:^{
    [self.view layoutIfNeeded];
}];

Swift:

self.someConstraint.constant = 10.0
UIView.animate(withDuration: 0.25, animations: self.view.layoutIfNeeded)
  1. Change the constraints and call [view layoutIfNeeded] inside an animation block. This interpolates between the two positions ignoring constraints during the animation.
[UIView animateWithDuration:0.5 animations:^{
    [view layoutIfNeeded];
}]
  1. Change the priority of the constraints. This is less CPU intensive than adding and removing constraints.
  2. Remove all constraints and use autosizing masks. For the later, you have to set view.translatesAutoresizingMaskIntoConstraints = YES.
  3. Use constraints that don’t interfere with the intended animation.
  4. Use a container view. Position the superview using constraints. Then add a subview with constraints that don’t fight the animation, eg: a center relative to the superview. This unloads part of the constraints to the superview, so they don’t fight the animation in the subview.
  5. Animate layers instead views. Layer transforms don’t trigger the Auto Layout.
CABasicAnimation* ba = [CABasicAnimation animationWithKeyPath:@"transform"];
ba.autoreverses = YES;
ba.duration = 0.3;
ba.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeScale(1.1, 1.1, 1)];
[v.layer addAnimation:ba forKey:nil];
  1. Override layoutSubviews. Call [super layoutSubviews] and fine tune the constraints.
  2. Change the frame in viewDidLayoutSubviews. Auto Layout is applied in layoutSubviews, so once done, change it in viewDidLayoutSubviews.
  3. Opt out from Auto Layout and set views manually. You can do this overriding layoutSubviews/layout without calling the super class’s implementation.

Quick tip: if the parent of the animated view is not being interpolated (that is, the animation jumps from beginning to end state), call layoutIfNeeded() in the deepest view that is the parent of the view that is animated (in other words, that is not affected by the animation). I don’t know exactly why this works.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:


Auto Layout:
* How to animate with Auto Layout

Table Of Contents
11 Auto Layout
12 UIView
15 UIColor
26 UIImage
28 CALayer
30 NSDate
40 iBeacon
49 NSTimer
79 NSURL
87 AWS SDK
96 NSData
101 Segues
104 EventKit
105 NSBundle
106 SiriKit
111 StoreKit
117 3D Touch
119 Keychain
122 Block
141 AirDrop
144 UISlider
145 Carthage
146 HealthKit
151 plist
157 MVVM
164 UIPhoenix
166 Simulator
168 NSArray
169 OpenGL
175 Core Data
179 MyLayout
180 UIFont
189 Security
200 Codable