I'm pretty new to auto layout and I'm confused about how to animate views.
I read a lot, and I know you must hold to the constraints, edit it, and wrap the layoutIfNeeded
in an UIView
animation block.
But when it comes to do it, I'm a little lost. I'd love if someone could explain me how this animation is done for example.
I think it probably uses a UIPanGestureRecognizer
to change the constant
of the leading space to container constraint, but it probably uses UIDynamics (for the bounce effect at the right ?).
Well, similar behavior could be achieved with
UIPanGestureRecognizer
+[UIView animateWithDuration:animations:]
. Yes, you set leading space constraint and change it according toUIPanGestureRecognizer
state. Remember that you need to set final constraints only (define final position of a slider). Intermediate animation positions are calculated for you. For the slider we have default left position and activated middle position.For view rotation we can use
transform
property ofUIView
.Autolayout constraints in IB:
Setting animation options (
UIViewAnimationOptionCurveEaseOut
animation curve) could give a feel of bounce effect.UIPanGestureRecognizer
code (omit instance variables declaration, because their names are self-explanatory):Animation result with
UIViewAnimationOptionCurveLinear
(capture simulator):Animation result with
UIViewAnimationOptionCurveEaseOut
(capture simulator):UIDynamics
With UIDynamics things become more complicated. Good starting point is Ray Wenderlich UIKit Dynamics Tutorial.
For bouncing slider we could add following behaviors:
UIGravityBehavior
which pulls a slider to start position. We need to changeangle
property to direct gravity force to the left.UICollisionBehavior
which defines left and right edges of allowed movements.translatesReferenceBoundsIntoBoundary
property will be useful if we treat parent view as boundary. Also we need to add extra boundary to stop slider in the middle usingaddBoundaryWithIdentifier:fromPoint:toPoint
(or bezier path).UIDynamicItemBehavior
to changeelasticy
and possiblyresistance
properties to configure bounce and acceleration respectively.UIPushBehavior
in conjunction with recognizer'svelocityInView:
to specify slider velocity when a user releases a sliderUISnapBehavior
as an alternative toUIGravityBehavior