UIGravityBehavior Class Reference

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

Overview

A gravity behavior specifies a gravity vector that applies to all of its dynamic items.

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.

The default value for the gravity vector is (0.0, 1.0), which correspond to the two component values you can apply using the the gravityDirection property. The acceleration for a dynamic item subject to a (0.0, 1.0) gravity vector, also known as standard UIKit gravity, is downwards at 1000 points / second².

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

  1. Associate the item with the behavior using the addItem: method, or initialize a new gravity 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 gravity 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 gravity behavior in a custom, composite behavior by starting with a UIDynamicBehavior object and adding a gravity behavior with the addChildBehavior: method. If you want to influence a gravity behavior at each step of a dynamic animation, implement the inherited action method.

Tasks

Initializing and Managing a Gravity Behavior

Configuring a Gravity Behavior

Properties

angle

The angle, in radians, of the gravity vector for the behavior.

@property(readwrite, nonatomic) CGFloat angle
Discussion

The radian angle for this property conforms to standard UIKit geometry. To configure a gravity vector for a gravity behavior, set the magnitude property as well as the angle property.

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

gravityDirection

The direction for the gravity behavior’s gravity vector, expressed as an (x, y) pair.

@property(readwrite, nonatomic) CGVector gravityDirection
Discussion

The default gravity vector expressed as an x, y pair is (0.0, 1.0), equivalent to no gravity component directed to the right and a downward component of 1000 points / second². A negative value in either component reverses the direction of the component.

Availability
  • Available in iOS 7.0 and later.
See Also
Declared In
UIGravityBehavior.h

items

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

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

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

magnitude

The magnitude of the gravity vector for the gravity behavior.

@property(readwrite, nonatomic) CGFloat magnitude
Discussion

A magnitude value of 1.0 represents an acceleration of 1000 points / second². To configure a gravity vector for a gravity behavior, set the angle property as well as the magnitude property.

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

Instance Methods

addItem:

Adds a dynamic item to the gravity behavior’s item array.

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

The dynamic item to add to the item array.

Discussion

All the dynamic items added to a gravity behavior are subject to the same gravity vector.

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

initWithItems:

Initializes a gravity behavior with an array of dynamic items.

- (instancetype)initWithItems:(NSArray *)items
Parameters
items

The dynamic items that you want to be subject to the gravity behavior.

Return Value

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

Discussion

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

removeItem:

Removes a specific dynamic item from the gravity behavior.

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

The dynamic item that you want to remove.

Discussion

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

setAngle:magnitude:

Sets the angle and magnitude of the gravity vector for the behavior.

- (void)setAngle:(CGFloat)angle magnitude:(CGFloat)magnitude
Parameters
angle

The radian angle for the gravity vector, using standard UIKit geometry.

magnitude

The acceleration for the gravity, with a value of 1.0 corresponding to 1000 points / second².

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