UIAttachmentBehavior Class Reference

Inherits from
Conforms to
Framework
/System/Library/Frameworks/UIKit.framework
Availability
Available in iOS 7.0 and later.
Declared in
UIAttachmentBehavior.h

Overview

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.

Tasks

Initializing and Managing an Attachment Behavior

Configuring an Attachment Behavior

Properties

anchorPoint

The anchor point for an attachment behavior of type UIAttachmentBehaviorTypeAnchor.

@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.

Availability
  • Available in iOS 7.0 and later.
Declared In
UIAttachmentBehavior.h

attachedBehaviorType

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

@property(readonly, nonatomic) UIAttachmentBehaviorType attachedBehaviorType
Discussion

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

Availability
  • Available in iOS 7.0 and later.
Declared In
UIAttachmentBehavior.h

damping

The amount of damping to apply to the attachment behavior.

@property(readwrite, nonatomic) CGFloat damping
Availability
  • Available in iOS 7.0 and later.
Declared In
UIAttachmentBehavior.h

frequency

The frequency of oscillation for the attachment behavior.

@property(readwrite, nonatomic) CGFloat frequency
Availability
  • Available in iOS 7.0 and later.
Declared In
UIAttachmentBehavior.h

items

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

@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.

Availability
  • Available in iOS 7.0 and later.
Declared In
UIAttachmentBehavior.h

length

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

@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.

Availability
  • Available in iOS 7.0 and later.
Declared In
UIAttachmentBehavior.h

Instance Methods

initWithItem:attachedToAnchor:

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

- (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.

Availability
  • Available in iOS 7.0 and later.
Declared In
UIAttachmentBehavior.h

initWithItem:attachedToItem:

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

- (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.

Availability
  • Available in iOS 7.0 and later.
Declared In
UIAttachmentBehavior.h

initWithItem:offsetFromCenter:attachedToAnchor:

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

- (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.

Availability
  • Available in iOS 7.0 and later.
Declared In
UIAttachmentBehavior.h

initWithItem:offsetFromCenter:attachedToItem:offsetFromCenter:

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

- (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.

Availability
  • Available in iOS 7.0 and later.
Declared In
UIAttachmentBehavior.h

Constants

UIAttachmentBehaviorType

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

typedef enum {
   UIAttachmentBehaviorTypeItems,
   UIAttachmentBehaviorTypeAnchor
} UIAttachmentBehaviorType;
Constants
UIAttachmentBehaviorTypeItems

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

Available in iOS 7.0 and later.

Declared in UIAttachmentBehavior.h.

UIAttachmentBehaviorTypeAnchor

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

Available in iOS 7.0 and later.

Declared in UIAttachmentBehavior.h.