iOS Developer Library

Developer

UIKit Framework Reference UIGravityBehavior Class Reference

Options
Deployment Target:

On This Page
Language:

UIGravityBehavior

Inheritance


Conforms To


Import Statement


Swift

import UIKit

Objective-C

@import UIKit;

Availability


Available in iOS 7.0 and later.

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.

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

    Declaration

    Swift

    func addItem(_ item: UIDynamicItem)

    Objective-C

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

    Import Statement

    Objective-C

    @import UIKit;

    Swift

    import UIKit

    Availability

    Available in iOS 7.0 and later.

  • Initializes a gravity behavior with an array of dynamic items.

    Declaration

    Swift

    init(items items: [AnyObject])

    Objective-C

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

    Import Statement

    Objective-C

    @import UIKit;

    Swift

    import UIKit

    Availability

    Available in iOS 7.0 and later.

  • Removes a specific dynamic item from the gravity behavior.

    Declaration

    Swift

    func removeItem(_ item: UIDynamicItem)

    Objective-C

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

    Parameters

    item

    The dynamic item that you want to remove.

    Discussion

    Import Statement

    Objective-C

    @import UIKit;

    Swift

    import UIKit

    Availability

    Available in iOS 7.0 and later.

  • items items Property

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

    Declaration

    Swift

    var items: [AnyObject] { get }

    Objective-C

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

    Discussion

    Import Statement

    Objective-C

    @import UIKit;

    Swift

    import UIKit

    Availability

    Available in iOS 7.0 and later.

  • angle angle Property

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

    Declaration

    Swift

    var angle: CGFloat

    Objective-C

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

    Import Statement

    Objective-C

    @import UIKit;

    Swift

    import UIKit

    Availability

    Available in iOS 7.0 and later.

  • magnitude magnitude Property

    The magnitude of the gravity vector for the gravity behavior.

    Declaration

    Swift

    var magnitude: CGFloat

    Objective-C

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

    Import Statement

    Objective-C

    @import UIKit;

    Swift

    import UIKit

    Availability

    Available in iOS 7.0 and later.

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

    Declaration

    Swift

    func setAngle(_ angle: CGFloat, magnitude magnitude: CGFloat)

    Objective-C

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

    Import Statement

    Objective-C

    @import UIKit;

    Swift

    import UIKit

    Availability

    Available in iOS 7.0 and later.

    See Also

    angle
    magnitude

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

    Declaration

    Swift

    var gravityDirection: CGVector

    Objective-C

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

    Import Statement

    Objective-C

    @import UIKit;

    Swift

    import UIKit

    Availability

    Available in iOS 7.0 and later.

    See Also

    angle