A dynamic behavior confers a behavioral configuration on one or more dynamic items for their participation in two-dimensional animation.


A dynamic item is any iOS or custom object that conforms to the UIDynamic​Item protocol. The UIView and UICollection​View​Layout​Attributes 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—an instance of the UIDynamic​Animator class.

This parent class, UIDynamic​Behavior, is inherited by the primitive dynamic behavior classes UIAttachment​Behavior, UICollision​Behavior, UIGravity​Behavior, UIDynamic​Item​Behavior, UIPush​Behavior, and UISnap​Behavior.

You can subclass UIDynamic​Behavior. By using the add​Child​Behavior(_:​) method in an instance of this class or in a custom subclass, you can create composite behaviors of your own design.

When you subclass UIDynamic​Behavior, you typically need to provide one or more initializers, along with other housekeeping methods such as those implemented in the iOS primitive dynamic behaviors.

To perform per-step logic in a dynamic animation, provide a block object using the action property.

To access the dynamic animator that a dynamic behavior is associated with, use the dynamic​Animator property. To respond to a dynamic behavior being added to or removed from a dynamic animator, implement the will​Move(to:​) method.


Configuring a Dynamic Behavior

var action:​ (() -> Void)?

The block you want to execute during dynamic animation.

func add​Child​Behavior(UIDynamic​Behavior)

Adds a dynamic behavior, as a child, to a custom dynamic behavior.

var child​Behaviors:​ [UIDynamic​Behavior]

Returns the array of dynamic behaviors that are children of a custom dynamic behavior.

func remove​Child​Behavior(UIDynamic​Behavior)

Removes a child dynamic behavior from a custom dynamic behavior.

Responding to Changes in the Behavior Tree

var dynamic​Animator:​ UIDynamic​Animator?

The dynamic animator that the dynamic behavior is associated with.

func will​Move(to:​ UIDynamic​Animator?)

Called when the dynamic behavior is added to, or removed from, a dynamic animator.


Inherits From