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:
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)
[view layoutIfNeeded]
inside an animation block. This interpolates between the two positions ignoring constraints during the animation.[UIView animateWithDuration:0.5 animations:^{
[view layoutIfNeeded];
}]
view.translatesAutoresizingMaskIntoConstraints = YES
.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];
[super layoutSubviews]
and fine tune the constraints.layoutSubviews
, so once done, change it in viewDidLayoutSubviews
.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.