iOS Developer Library — Prerelease

Developer

UIKit Framework Reference UICollisionBehavior Class Reference

Options
Deployment Target:

On This Page
Language:

UICollisionBehavior

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 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 initWithItems: 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 Class Reference.

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 addBoundaryWithIdentifier:forPath: method) or with a line segment (see the addBoundaryWithIdentifier:fromPoint:toPoint: 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 setTranslatesReferenceBoundsIntoBoundaryWithInsets: 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.

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

    Declaration

    Swift

    func addItem(_ item: UIDynamicItem)

    Objective-C

    - (void)addItem:(id<UIDynamicItem> _Nonnull)item

    Parameters

    item

    The dynamic item to add to the item array.

    Discussion

    You can add a dynamic item to one or more collision behaviors. For example, you can use two collision behaviors to specify that item A can collide with item B and that item C can collide with item D, but that items A and B ignore items C and D.

    There is no hard limit to the number of dynamic items you can add to a collision behavior. However, adding a large number of items might result in a performance impact. Be sure to test your behaviors on the device configurations you are targeting.

    Availability

    Available in iOS 7.0 and later.

  • init(items:) - initWithItems: Designated Initializer

    Initializes a collision behavior with an array of dynamic items.

    Declaration

    Swift

    init(items items: [UIDynamicItem])

    Objective-C

    - (instancetype _Nonnull)initWithItems:(NSArray<id<UIDynamicItem>> * _Nonnull)items

    Parameters

    items

    The dynamic items that you want to participate in the collision behavior.

    Return Value

    The initialized collision behavior, or nil if there was a problem initializing the object.

    Availability

    Available in iOS 7.0 and later.

  • Removes a specific dynamic item from the collision behavior.

    Declaration

    Swift

    func removeItem(_ item: UIDynamicItem)

    Objective-C

    - (void)removeItem:(id<UIDynamicItem> _Nonnull)item

    Parameters

    item

    The dynamic item that you want to remove.

    Availability

    Available in iOS 7.0 and later.

  • Returns the set of dynamic items you’ve added to the collision behavior. (read-only)

    Declaration

    Swift

    var items: [UIDynamicItem] { get }

    Objective-C

    @property(nonatomic, readonly, copy) NSArray <id<UIDynamicItem>> * _Nonnull items

    Availability

    Available in iOS 7.0 and later.

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

    Declaration

    Swift

    struct UICollisionBehaviorMode : OptionSetType { init(rawValue rawValue: UInt) static var Items: UICollisionBehaviorMode { get } static var Boundaries: UICollisionBehaviorMode { get } static var Everything: UICollisionBehaviorMode { get } }

    Objective-C

    typedef NS_OPTIONS (NSUInteger, UICollisionBehaviorMode ) { UICollisionBehaviorModeItems = 1 << 0, UICollisionBehaviorModeBoundaries = 1 << 1, UICollisionBehaviorModeEverything = NSUIntegerMax };

    Constants

    • Items

      UICollisionBehaviorModeItems

      Specifies that the dynamic items, associated with the collision behavior, collide only with each other and not with specified collision boundaries.

      Available in iOS 7.0 and later.

    • Boundaries

      UICollisionBehaviorModeBoundaries

      Specifies that the dynamic items, associated with the collision behavior, collide only with specified collision boundaries and do not collide with each other.

      Available in iOS 7.0 and later.

    • Everything

      UICollisionBehaviorModeEverything

      Specifies that the dynamic items, associated with the collision behavior, collide with each other and with specified collision boundaries.

      This is the default collision behavior mode.

      Available in iOS 7.0 and later.

    Import Statement

    Objective-C

    @import UIKit;

    Swift

    import UIKit

    Availability

    Available in iOS 7.0 and later.