A collision behavior confers, to a specified array of dynamic items, the ability of those items to engage in collisions with each other and with the behavior’s specified boundaries. A collision behavior also specifies some characteristics of its items’ collisions, with other characteristics optionally specified by a UIDynamic​Item​Behavior object.


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 use a custom object as a dynamic item for such purposes as reacting to rotation or position changes computed by a dynamic animator—an instance of the UIDynamic​Animator class.

To use a collision behavior with a dynamic item, perform these two steps:

  1. Associate the item with the behavior using the add​Item(_:​) method, or initialize a new collision behavior with an array of items using the init(items:​) method.

  2. Enable the behavior by adding it to an animator using the add​Behavior(_:​) method

The coordinate system that pertains to a collision behavior, and the types of dynamic items you can use with the behavior, depend on how you initialized the associated animator. For details, read the Overview of UIDynamic​Animator.

You can add multiple collision behaviors to a dynamic animator. A dynamic item can be part of any number of collision behaviors, provided those behaviors belong to the same animator. For example, you can specify a collision behavior for a set of say, blue, items and another for, say, pink items. When you add both behaviors to a dynamic animator, blue items can collide with each other and pink items can collide with each other, but a blue item and a pink item would not collide—they would ignore each other.

By default, a collision behavior’s items can collide with each other and with any boundaries you’ve specified for the behavior. If you want to specify that a behavior’s items collide only with each other, or only with boundaries, explicitly set the collision​Mode property.

You can define a collision boundary with a bezier path (see the add​Boundary(with​Identifier:​for:​) method) or with a line segment (see the add​Boundary(with​Identifier:​from:​to:​) method). When you use a collision behavior with a dynamic animator you’ve initialized with a reference view or a collection view layout, you can also specify a collision boundary according to the bounds of the dynamic animator’s coordinate system (see the set​Translates​Reference​Bounds​Into​Boundary(with:​) method).

To respond to collisions, implement a delegate object that adopts the UICollision​Behavior​Delegate protocol. Add the delegate to the behavior using the collision​Delegate property.

You can include a collision behavior in a custom, composite behavior by starting with a UIDynamic​Behavior object and adding a collision behavior with the add​Child​Behavior(_:​) method. If you want to influence a collision behavior at each step of a dynamic animation, implement the inherited action method.


Initializing and Managing a Collision Behavior

func add​Item(UIDynamic​Item)

Adds a dynamic item to the collision behavior’s item array.

init(items:​ [UIDynamic​Item])

Initializes a collision behavior with an array of dynamic items.

func remove​Item(UIDynamic​Item)

Removes a specific dynamic item from the collision behavior.

var items:​ [UIDynamic​Item]

Returns the set of dynamic items you’ve added to the collision behavior.

Configuring a Collision Behavior

func add​Boundary(with​Identifier:​ NSCopying, for:​ UIBezier​Path)

Adds a collision boundary, specified as a Bezier path, to the collision behavior.

func add​Boundary(with​Identifier:​ NSCopying, from:​ CGPoint, to:​ CGPoint)

Adds a collision boundary, specified as a line segment, to the collision behavior.

var boundary​Identifiers:​ [NSCopying]?

The set of boundary identifiers that you’ve added to the collision behavior.

func boundary(with​Identifier:​ NSCopying)

Returns a specified Bezier-path boundary.

var collision​Delegate:​ UICollision​Behavior​Delegate?

The delegate object that you want to respond to collisions for the collision behavior.

var collision​Mode:​ UICollision​Behavior​Mode

The type of edges that participate in collisions for the collision behavior.

func remove​All​Boundaries()

Removes all previously-specified collision boundaries from the collision behavior.

func remove​Boundary(with​Identifier:​ NSCopying)

Removes a specific collision boundary from the collision behavior.

func set​Translates​Reference​Bounds​Into​Boundary(with:​ UIEdge​Insets)

Specifies a collision boundary based on the bounds of the animation reference system, with optional insets.

var translates​Reference​Bounds​Into​Boundary:​ Bool

Specifies whether a boundary based on the reference system is active.



The types of edges that participate in collisions for a collision behavior.