Skip to content
Titanium SDK
Version13.0.1

You are reading 13.0.1. See this page in 13.4.1.

Titanium.UI.iOS.GravityBehavior

proxyiPhoneiPadmacOSsince iPhone 3.2.0, iPad 3.2.0, macOS 9.2.0

Extends Titanium.Proxy

Gravitational force to apply to an item.

A gravity behavior configures the gravity vector of one or more items. To define a gravity behavior:

  1. Use the Titanium.UI.iOS.createGravityBehavior method to create and define the behavior.
  2. To define a gravity vector, either set the angle and magnitude properties, or set the gravityDirection property.
  3. Use the addItem method to add items to the behavior.
  4. Add the behavior to an Animator object.

You can only define one gravity behavior per animator.

Examples

Simple Example

The following example creates many random blocks. The initial gravitational force pulls the blocks downward. Clicking on the Change button generates a new random gravity vector, which pulls the blocks in a random direction.

var win = Ti.UI.createWindow({backgroundColor: 'white', fullscreen: true});

// Create an Animator object using the window as the coordinate system
var animator = Ti.UI.iOS.createAnimator({referenceView: win});

// Create a default collision behavior, using the window edges as boundaries
var collision = Ti.UI.iOS.createCollisionBehavior();

// Simulate Earth's gravity
var gravity = Ti.UI.iOS.createGravityBehavior({
    gravityDirection: {x: 0.0, y: 1.0}
});

var WIDTH = Ti.Platform.displayCaps.platformWidth;
var HEIGHT = Ti.Platform.displayCaps.platformHeight;

// Create a bunch of random blocks; add to the window and behaviors
var blocks = [];
for (var i = 0; i < 20; i++) {
    var r = Math.round(Math.random() * 255);
    var g = Math.round(Math.random() * 255);
    var b = Math.round(Math.random() * 255);
    var rgb = 'rgb(' + r +"," + g + "," + b + ")";

    blocks[i] = Ti.UI.createView({
        width: 25,
        height: 25,
        top: Math.round(Math.random() * (HEIGHT - 25) + 25),
        left: Math.round(Math.random() * (WIDTH - 25) + 25),
        backgroundColor: rgb
    });
    win.add(blocks[i]);
    collision.addItem(blocks[i]);
    gravity.addItem(blocks[i]);
}

animator.addBehavior(collision);
animator.addBehavior(gravity);
            
// Start the animation when the window opens
win.addEventListener('open', function(e){
    animator.startAnimator();
});

// Change the gravity vector when the button is clicked
var button = Ti.UI.createButton({title: 'Change'});
button.addEventListener('click', function(e){
    gravity.gravityDirection = {
        x: (1 - Math.random() * 2),
        y: (1 - Math.random() * 2)
    };
    Ti.API.info('gravity vector: ' + JSON.stringify(gravity.gravityDirection));
});
win.add(button);
win.open();

Properties 6

angle

Number

Specifies the angle of the gravity vector in radians.

To configure the gravity vector, you need to also specify the magnitude property.

Default: 0

apiName

Stringsince Android 3.2.0, iPhone 3.2.0, iPad 3.2.0, macOS 9.2.0read-only

Inherited from Titanium.Proxy

The name of the API that this proxy corresponds to.

bubbleParent

Booleansince Android 3.0.0, iPhone 3.0.0, iPad 3.0.0, macOS 9.2.0

Inherited from Titanium.Proxy

Indicates if the proxy will bubble an event to its parent.

Default: true

Specifies the direction of the gravity vector as an x, y pair.

For example, specifiying {x: 0.0, y: 1.0} indicates a downward gravity force of 1000 points per second squared.

Default: (0, 0)

magnitude

Number

Specifies the magnitude of the gravity vector.

A value of 1.0 represents an acceleration of 1000 points per second squared.

To configure the gravity vector, you need to also specify the angle property.

Default: 0

Methods 6

Inherited from Titanium.Proxy

Adds the specified callback as an event listener for the named event.

Parameters
ParametersTypeDescription
nameString

Name of the event.

callbackCallback<Titanium.Event>

Callback function to invoke when the event is fired.

4 fields
bubbles Boolean

True if the event will try to bubble up if possible.

cancelBubble Boolean

Set to true to stop the event from bubbling.

source Object

Source object that fired the event.

type String

Name of the event fired.

Adds an item to this behavior.

Parameters
ParametersTypeDescription
itemTitanium.UI.View

View object to add to the behavior.

Inherited from Titanium.Proxy

Fires a synthesized event to any registered listeners.

Parameters
ParametersTypeDescription
nameString

Name of the event.

event?Dictionary

A dictionary of keys and values to add to the Titanium.Event object sent to the listeners.

Inherited from Titanium.Proxy

Removes the specified callback as an event listener for the named event.

Parameters
ParametersTypeDescription
nameString

Name of the event.

callbackCallback<Titanium.Event>

Callback function to remove. Must be the same function passed to addEventListener.

4 fields
bubbles Boolean

True if the event will try to bubble up if possible.

cancelBubble Boolean

Set to true to stop the event from bubbling.

source Object

Source object that fired the event.

type String

Name of the event fired.