Skip to content
Titanium SDK
Version12.5.0

You are reading 12.5.0. See this page in 13.4.1.

Titanium.UI.iOS.PushBehavior

proxyiPhoneiPadmacOSsince iPhone 3.2.0, iPad 3.2.0, macOS 9.2.0

Extends Titanium.Proxy

Continuous or instantaneous force to apply to an item.

A push behavior configures the continuous or instaneous force to apply to one or more items. To define a push behavior:

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

Examples

Simple Example

The following example generates a random push force on the block when it stops.

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});

var block = Ti.UI.createView({
    width: 100,
    height: 100,
    backgroundColor: 'blue',
    transform: Ti.UI.createMatrix2D({ rotate: 45 })
});

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

// Push the block down when the application first starts
var push = Ti.UI.iOS.createPushBehavior({
    pushDirection: {x: 0.0, y: 1.0},
    pushMode: Ti.UI.iOS.PUSH_MODE_INSTANTANEOUS
});
push.addItem(block);
animator.addBehavior(push);

// Apply a new push behavior when the item stops
animator.addEventListener('pause', function(e){
    push.angle = 2 * Math.PI * Math.random();
    push.magnitude = Math.random() * 5 + 5;
    push.active = true;
});

animator.addEventListener('resume', function(e){
    Ti.API.info(JSON.stringify(
        'push force: ' + push.magnitude * 100 + " points/s^2 @ "
        + (push.angle * 360 / (2 * Math.PI)) + " degrees")
    );
});

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

win.add(block);
win.open();

Properties 8

active

Boolean

State of the push behavior's force.

Set to true to activate the force or false to deactivate it. Use this property rather than removing and re-adding the behavior to the animator.

Default: true

angle

Number

Specifies the angle of the force vector in radians.

To configure the force 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

magnitude

Number

Specifies the magnitude of the force vector.

A value of 1.0 represents an acceleration of 100 points per second squared if the item is 100 x 100 points with a density of 1.0.

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

Default: 0

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

For example, specifiying {x: 0.0, y: 1.0} indicates a positive upward force of 100 points per second squared if the item is 100 x 100 points with a density of 1.0.

Specifiying a negative value reverses the direction of the force.

Default: (0,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.