An object that applies a gravity-like force to all of its associated dynamic items.
SDKs
- iOS 7.0+
- tvOS 9.0+
Framework
- UIKit
Declaration
class UIGravityBehavior : UIDynamic Behavior
Overview
The magnitude and direction of the gravity force are configurable and are applied equally to all associated items. Use this behavior to modify the position of views and other dynamic items in your app’s interface.
You specify the magnitude and direction of the gravity force as a two-dimensional vector in the reference view’s coordinate system. A value of 1
imparts the standard UIKit gravity, which corresponds to an acceleration of 1000 points / second² in the direction of the given axis. The default vector in the gravity
property is (0
, 1
), which causes items to accelerate downward along the positive y axis. If you prefer to set the vector angle
and magnitude
separately, you can do so using the corresponding properties.
Note
If you want a gravitational force that takes the mass of each object into account, use a UIField
object instead. Field behaviors support both linear and radial gravitation fields and take the mass of an object into account, resulting in different amounts of force applied to each object.
If you want to influence a gravity behavior at each step of a dynamic animation, assign an appropriate block to the inherited action
property. Use your block to make any needed changes to the dynamic items associated with the gravity behavior.
Applying a Gravity Behavior to an Item
To apply a gravity behavior to one or more dynamic items, do the following:
Initialize the behavior using the
init(items:)
method, passing in the items you want associated with the behavior. Use theadd
method to add items after initialization. For more information about dynamic items, seeItem(_:) UIDynamic
.Item Enable the gravity behavior by adding it to your
UIDynamic
object using theAnimator add
method. (Do not add more than one gravity behavior to an animator.)Behavior(_:)
The gravity behavior derives its coordinate system from the reference view of its associated dynamic animator object. For more information about the dynamic animator and the reference coordinate system, see UIDynamic
.