UIDynamicAnimator

UIDynamicAnimator can be taken as an isolated physical environment, where objects receive physical impact (UIDynamicItem) when it’s added to animator.

Definition

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: UIAttachmentBehaviorUICollisionBehaviorUIDynamicItemBehaviorUIGravityBehaviorUIPushBehavior, 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:

  1. 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 centertransform, and bounds properties in the case of a UIView-based item)
  2. 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)
  3. 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.

UIDynamicBehavior

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.

Share this post

Subscribe for “The Next Bytes” where Han & the crew draft up our observation in the industry.

We’d love to work with you.

Drop us a message if you need any helps from the Dwarves

Let’s build with us