A dynamic animator provides physics-related capabilities and animations for its dynamic items, and provides the context for those animations. It does this by intermediating between the underlying iOS physics engine and dynamic items, via behavior objects you add to the animator.
- iOS 7.0+
- tvOS 7.0+
A dynamic item is any iOS or custom object that conforms to the
UIDynamicItem protocol. The
UICollectionViewLayoutAttributes classes implement this protocol starting in iOS 7.0. You can implement this protocol to use a dynamic animator with custom objects for such purposes as reacting to rotation or position changes computed by an animator.
To use dynamics, configure one or more dynamic behaviors—including providing each with a set of dynamic items—and then add those behaviors to a dynamic animator.
You specify dynamic behaviors using any of the iOS primitive dynamic behavior classes:
UISnapBehavior. Each of these provides configuration options and lets you associate one or more dynamic items to the behavior. To activate a behavior, add it to an animator.
A dynamic animator interacts with each of its dynamic items as follows:
Before adding an item to a behavior, you specify the item’s starting position, rotation, and bounds (to do so, use properties of the item’s class, such as the
boundsproperties in the case of a
After you add the behavior to an animator, the animator takes over: it updates the item’s position and rotation as animation proceeds (see the
You can programmatically update an item’s state in the midst of an animation, after which the animator takes back control of the item’s animation, relative to the state you specified (see the
You can define composite behaviors using the
addChildBehavior(_:) method of the
UIDynamicBehavior parent behavior class. The set of behaviors you add to an animator constitute a behavior hierarchy. Each behavior instance you associate with an animator can be present only once in the hierarchy.
To employ a dynamic animator, first identify the type of dynamic items you want to animate. This choice determines which initializer to call, and this in turn determines how the coordinate system gets set up. The three ways to initialize an animator, the dynamic items you can then use, and the resulting coordinate system, are as follows:
To animate views, create an animator with the
init(referenceView:)method. The coordinate system of the reference view serves as the coordinate system for the animator’s behaviors and items. Each dynamic item you associate with this sort of animator must be a
UIViewobject and must descend from the reference view.
You can define a boundary, for items participating in a collision behavior, relative to the bounds of the reference view. See the
To animate collection views, create an animator with the
init(collectionViewLayout:)method. The resulting animator employs a collection view layout (an object of the
UICollectionViewLayoutclass) for its coordinate system. The dynamic items in this sort of animator must be
UICollectionViewLayoutAttributesobjects that are part of the layout.
You can define a boundary, for items participating in a collision behavior, relative to the bounds of the collection view layout. See the
A collection view animator automatically calls the
invalidateLayout()method as needed, and automatically pauses and resumes animation, as appropriate, when you change a collection view’s layout.
To employ a dynamic animator with other objects that conform to the
UIDynamicItemprotocol, create an animator with the inherited
init()method. The resulting animator employs an abstract coordinate system, not tied to the screen or to any view.
There is no reference boundary to refer to when defining a collision boundary for use with this sort of animator. However, you can still, in a collision behavior, specify custom boundaries as described in
All types of dynamic animators share the following characteristics:
Each dynamic animator is independent of other dynamic animators you create
You can associate a given dynamic item with multiple behaviors, provided those behaviors belong to the same animator
An animator automatically pauses when all its items are at rest, and automatically resumes when a behavior parameter changes or a behavior or item is added or removed
You can implement a delegate to respond to changes in animator pause/resumption status, using the
dynamicAnimatorWillResume(_:) methods of the