Contents

UICollisionBehavior

An object that confers to a specified array of dynamic items the ability to engage in collisions with each other and with the behavior’s specified boundaries.

Declaration

@MainActor class UICollisionBehavior

Overview

A collision behavior also specifies some characteristics of its items’ collisions, with other characteristics optionally specified by a UIDynamicItemBehavior object. A dynamic item is any iOS or custom object that conforms to the UIDynamicItem protocol. The UIView and UICollectionViewLayoutAttributes 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 UIDynamicAnimator class.

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

  1. Associate the item with the behavior using the addItem(_:) 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 addBehavior(_:) 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 UIDynamicAnimator.

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 collisionMode property.

You can define a collision boundary with a bezier path (see the addBoundary(withIdentifier:for:) method) or with a line segment (see the addBoundary(withIdentifier: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 setTranslatesReferenceBoundsIntoBoundary(with:) method).

To respond to collisions, implement a delegate object that adopts the UICollisionBehaviorDelegate protocol. Add the delegate to the behavior using the collisionDelegate property.

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

Topics

Initializing and managing a collision behavior

Customizing the collision behavior

Configuring a collision behavior

Constants

See Also

Behaviors