UISnapBehavior Class Reference

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

Overview

A snap behavior defines a dynamic item’s movement to a specified point; the movement proceeds with a spring-like effect, ending with an oscillation whose amount you can set.

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 snap behavior with a dynamic item, perform these two steps:

  1. Initialize a new snap behavior with the item using the initWithItem:snapToPoint: method

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

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

Tasks

Initializing a Snap Behavior

Configuring a Snap Behavior

Properties

damping

The amount of oscillation of a dynamic item during the conclusion of a snap.

@property(nonatomic, assign) CGFloat damping
Discussion

The valid range for damping extends from 0.0, for minimum oscillation, through 1.0, for maximum oscillation. The default value is 0.5.

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

Instance Methods

initWithItem:snapToPoint:

Initializes a snap behavior with a dynamic item and a snap point.

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

The dynamic item that you want to apply a snap behavior to.

point

The point that you want the dynamic item to snap to. The coordinate system for the point parameter depends on how you initialize the dynamic animator you’re adding the snap behavior to, as described in the overview of UIDynamicAnimator Class Reference.

Return Value

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

Discussion

At the conclusion of a snap, the rotation value (as indicated by the transform property) for a dynamic item is 0.

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