iOS Developer Library

Developer

UIKit Framework Reference UIAttachmentBehavior Class Reference

Options
Deployment Target:

On This Page
Language:

UIAttachmentBehavior

An attachment behavior specifies a dynamic connection between two dynamic items, or between a dynamic item and an anchor point. By default, an item’s attachment point is at its center, but you can change that.

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.

When a dynamic item moves, either by tracking a gesture or via other input, any attached dynamic item also moves—if possible given its other dynamic parameters and boundaries. You can configure an attachment behavior using its length, damping, and frequency properties.

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

  1. Initialize a new attachment behavior with one or two items using one of the initWithItem:… methods of this class

  2. Enable the behavior by adding it to an animator using the addBehavior: method

The coordinate system that pertains to an attachment 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 include an attachment behavior in a custom, composite behavior by starting with a UIDynamicBehavior object and adding an attachment behavior with the addChildBehavior: method. If you want to influence an attachment behavior at each step of a dynamic animation, implement the inherited action method.

Inheritance


Conforms To


Import Statement


Swift

import UIKit

Objective-C

@import UIKit;

Availability


Available in iOS 7.0 and later.
  • items items Property

    The dynamic items connected by the attachment behavior. (read-only)

    Declaration

    Swift

    var items: [AnyObject] { get }

    Objective-C

    @property(nonatomic, readonly, copy) NSArray *items

    Discussion

    Contains two elements when used for an attachment behavior of type UIAttachmentBehaviorTypeItems; contains one element when used for an attachment behavior of type UIAttachmentBehaviorTypeAnchor.

    Import Statement

    Objective-C

    @import UIKit;

    Swift

    import UIKit

    Availability

    Available in iOS 7.0 and later.

  • Initializes an attachment behavior that connects the center point of a dynamic item to an anchor point.

    Declaration

    Swift

    convenience init!(item item: UIDynamicItem, attachedToAnchor point: CGPoint)

    Objective-C

    - (instancetype)initWithItem:(id<UIDynamicItem>)item attachedToAnchor:(CGPoint)point

    Parameters

    item

    The dynamic item to which you are applying an attachment behavior.

    point

    The anchor point for the attachment behavior, relative to the coordinate system for the behavior’s associated dynamic animator. For more information, see the Overview in UIDynamicAnimator Class Reference.

    Return Value

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

    Discussion

    Used for attachment behaviors of type UIAttachmentBehaviorTypeAnchor.

    Import Statement

    Objective-C

    @import UIKit;

    Swift

    import UIKit

    Availability

    Available in iOS 7.0 and later.

  • Initializes an attachment behavior that connects the center point of a dynamic item to the center point of another dynamic item.

    Declaration

    Swift

    convenience init!(item item1: UIDynamicItem, attachedToItem item2: UIDynamicItem)

    Objective-C

    - (instancetype)initWithItem:(id<UIDynamicItem>)item1 attachedToItem:(id<UIDynamicItem>)item2

    Parameters

    item1

    The first of two dynamic items connected by the attachment behavior.

    item2

    The second of two dynamic items connected by the attachment behavior.

    Return Value

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

    Discussion

    Used for attachment behaviors of type UIAttachmentBehaviorTypeItems.

    Import Statement

    Objective-C

    @import UIKit;

    Swift

    import UIKit

    Availability

    Available in iOS 7.0 and later.

  • Initializes an attachment behavior that connects a specified point in a dynamic item to an anchor point.

    Declaration

    Swift

    convenience init!(item item: UIDynamicItem, offsetFromCenter p1: UIOffset, attachedToAnchor point: CGPoint)

    Objective-C

    - (instancetype)initWithItem:(id<UIDynamicItem>)item offsetFromCenter:(UIOffset)p1 attachedToAnchor:(CGPoint)point

    Parameters

    item

    The dynamic item to which you are applying an attachment behavior.

    p1

    The point, within the dynamic item and described as an offset from its center point, for the attachment behavior.

    point

    The anchor point for the attachment behavior, relative to the coordinate system for the behavior’s associated dynamic animator. For more information, see the Overview in UIDynamicAnimator Class Reference.

    Return Value

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

    Discussion

    Used for attachment behaviors of type UIAttachmentBehaviorTypeAnchor.

    Import Statement

    Objective-C

    @import UIKit;

    Swift

    import UIKit

    Availability

    Available in iOS 7.0 and later.

  • Initializes an attachment behavior that connects a specified point in one dynamic item to a specified point in another dynamic item.

    Declaration

    Swift

    init!(item item1: UIDynamicItem, offsetFromCenter p1: UIOffset, attachedToItem item2: UIDynamicItem, offsetFromCenter p2: UIOffset)

    Objective-C

    - (instancetype)initWithItem:(id<UIDynamicItem>)item1 offsetFromCenter:(UIOffset)p1 attachedToItem:(id<UIDynamicItem>)item2 offsetFromCenter:(UIOffset)p2

    Parameters

    item1

    The first of two dynamic items connected by the attachment behavior.

    p1

    The point, within the first dynamic item and described as an offset from its center point, for the attachment.

    item2

    The second of two dynamic items connected by the attachment behavior.

    p2

    The point, within the second dynamic item and described as an offset from its center point, for the attachment.

    Return Value

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

    Discussion

    This is the designated initializer for the UIAttachmentBehavior class.

    Import Statement

    Objective-C

    @import UIKit;

    Swift

    import UIKit

    Availability

    Available in iOS 7.0 and later.

  • The anchor point for an attachment behavior of type UIAttachmentBehaviorTypeAnchor.

    Declaration

    Swift

    var anchorPoint: CGPoint

    Objective-C

    @property(readwrite, nonatomic) CGPoint anchorPoint

    Discussion

    The anchor point is relative to the coordinate system for the behavior’s associated dynamic animator. For more information, see the Overview in UIDynamicAnimator Class Reference.

    Import Statement

    Objective-C

    @import UIKit;

    Swift

    import UIKit

    Availability

    Available in iOS 7.0 and later.

  • The type of the attachment behavior. (read-only)

    Declaration

    Swift

    var attachedBehaviorType: UIAttachmentBehaviorType { get }

    Objective-C

    @property(readonly, nonatomic) UIAttachmentBehaviorType attachedBehaviorType

    Discussion

    The available types for attachment behaviors are described in the UIAttachmentBehaviorType enum.

    Import Statement

    Objective-C

    @import UIKit;

    Swift

    import UIKit

    Availability

    Available in iOS 7.0 and later.

  • damping damping Property

    The amount of damping to apply to the attachment behavior.

    Declaration

    Swift

    var damping: CGFloat

    Objective-C

    @property(readwrite, nonatomic) CGFloat damping

    Import Statement

    Objective-C

    @import UIKit;

    Swift

    import UIKit

    Availability

    Available in iOS 7.0 and later.

  • frequency frequency Property

    The frequency of oscillation for the attachment behavior.

    Declaration

    Swift

    var frequency: CGFloat

    Objective-C

    @property(readwrite, nonatomic) CGFloat frequency

    Import Statement

    Objective-C

    @import UIKit;

    Swift

    import UIKit

    Availability

    Available in iOS 7.0 and later.

  • length length Property

    The distance, in points, between the two attachment points of the attachment behavior.

    Declaration

    Swift

    var length: CGFloat

    Objective-C

    @property(readwrite, nonatomic) CGFloat length

    Discussion

    Use this property to adjust the attachment length, if you want to, after creating an attachment. The system sets initial length automatically based on how you initialize the attachment.

    Import Statement

    Objective-C

    @import UIKit;

    Swift

    import UIKit

    Availability

    Available in iOS 7.0 and later.

  • The type of an attachment behavior, indicating what a dynamic item is attached to.

    Declaration

    Swift

    enum UIAttachmentBehaviorType : Int { case Items case Anchor }

    Objective-C

    typedef enum { UIAttachmentBehaviorTypeItems, UIAttachmentBehaviorTypeAnchor } UIAttachmentBehaviorType;

    Constants

    • Items

      UIAttachmentBehaviorTypeItems

      Designates an attachment behavior that connects a dynamic item to another dynamic item.

      Available in iOS 7.0 and later.

    • Anchor

      UIAttachmentBehaviorTypeAnchor

      Designates an attachment behavior that connects a dynamic item to an anchor point.

      Available in iOS 7.0 and later.

    Import Statement

    Objective-C

    @import UIKit;

    Swift

    import UIKit

    Availability

    Available in iOS 7.0 and later.