Definition
AĀ dynamic itemĀ is any iOS or custom object that conforms to theĀ UIDynamicItemĀ protocol. TheĀ UIViewĀ andĀ UICollectionViewLayoutAttributesĀ classes implement this protocol in iOS 7 and later. 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:Ā UIAttachmentBehavior,Ā UICollisionBehavior,Ā UIDynamicItemBehavior,Ā UIGravityBehavior,Ā UIPushBehavior, andĀ 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Ā center,Ā transform, andĀ boundsĀ properties in the case of a UIView-based item)
- 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Ā UIDynamicItemĀ protocol)
- 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Ā updateItem(usingCurrentState:)Ā method)
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Ā UIViewĀ object 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Ā setTranslatesReferenceBoundsIntoBoundary(with:)Ā method.
- To animate collection views, create an animator with theĀ init(collectionViewLayout:)Ā method. The resulting animator employs a collection view layout (an object of theĀ UICollectionViewLayoutĀ class) for its coordinate system. The dynamic items in this sort of animator must beĀ UICollectionViewLayoutAttributesĀ objects 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Ā setTranslatesReferenceBoundsIntoBoundary(with:)Ā method.
- 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Ā UICollisionBehavior.
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Ā dynamicAnimatorDidPause(_:)Ā andĀ dynamicAnimatorWillResume(_:)methods of theĀ UIDynamicAnimatorDelegateĀ protocol.
Key features
To create real-life animation experience, Apple simulates it using UIDynamicAnimator
Features
- Very good in term performance
- Easy to simulate realtime physic
- Make user feel animation very natural
- Only apply for 2D environment.
UIDynamicAnimator can be taken as an isolated physical environment, where objects receive physical impact (UIDynamicItem) when itās added to animator.
There are 5 types of Behavior
- UIGravityBehavior
- UICollisionBehavior
- UISnapBehavior
- UIPushBehavior
- UIAttachmentBehavior
Besides, UIDynamicItem(object) features can also adjust the elasticity of the object
- Elasticity
- Density
- Friction
- Resistance
Combine all of these factors, we end up with real-life physical interaction simulation.
UI dynamic behavior
A field behavior defines an area in which forces such as gravity, magnetism, drag, velocity, turbulence, and others can be applied. After creating a field behavior object of the appropriate type, configure the strength of the intended force along with any other field attributes.
After creating a field behavior object, call theĀ addItem(_:)Ā method to associate the field with that item. For many types of fields, you must also configure aĀ UIDynamicItemBehaviorobject for the item to define relevant attributes of the item such as its density (mass) or charge. After configuring the field, add it to theĀ UIDynamicAnimatorĀ object associated with your interface to begin the animations.
TheĀ positionĀ of a field defines its location in your interface and the fieldāsĀ regionĀ defines its area of influence. The region you specify is centered on the fieldās position. Regions can be circular or rectangular, and you can combine regions in different ways to create more complex region shapes.
Most fields use only a subset of the field attributes in their computations. All fields have a strengthĀ value that helps define the intensity of the field. Most fields also use theĀ falloffproperty to vary the field strength over distance. You configure other attributes only as needed for the type of field.