Dwarves
Memo
Type ESC to close search bar

Uidynamicanimator

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:

All types of dynamic animators share the following characteristics:

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

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

Besides, UIDynamicItem(object) features can also adjust the elasticity of the object

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.