Titanium.UI.iOS.CollisionBehavior
> Titanium.UI.iOS.CollisionBehavior

Dynamic behavior to support collisions between items and boundaries.

Requires: iOS 7.0 and later

The following APIs are supported on iOS 7 and later.

A collision behavior specifies the behavior when items collide with each other and boundaries. To define a collision behavior:

  1. Use the Titanium.UI.iOS.createCollisionBehavior method to create and define the behavior.
  2. Use the addItem method to add items to the behavior.
  3. Use the addBoundary method to add custom boundaries for the item to collide with. By default, the behavior uses the Animator object's reference view as the boundary.
  4. Add the behavior to an Animator object.

Examples

Simple Example

The following example creates many blocks scattered across the top of the window, which start falling after the window opens. The item and boundary collisions are reported to the console.

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

// Listen for collisions
function report(e) {
    Ti.API.info(JSON.stringify(e.type));
};
collision.addEventListener('itemcollision', report);
collision.addEventListener('boundarycollision', report);

// 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 < 25; 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 / 4) + 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();
});

win.open();
  • 3.2
  • 3.2
Defined By

Properties

apiName : Stringreadonly

The name of the API that this proxy corresponds to.

The name of the API that this proxy corresponds to.

The value of this property is the fully qualified name of the API. For example, Button returns Ti.UI.Button.

  • 3.2.0
  • 3.2.0
  • 3.2.0
Titanium.UI.iOS.CollisionBehavior
boundaryIdentifiers : BoundaryIdentifier[]readonly

Boundary identfiers added to this behavior.

Boundary identfiers added to this behavior.

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

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

Some proxies (most commonly views) have a relationship to other proxies, often established by the add() method. For example, for a button added to a window, a click event on the button would bubble up to the window. Other common parents are table sections to their rows, table views to their sections, and scrollable views to their views. Set this property to false to disable the bubbling to the proxy's parent.

Default: true

  • 3.0.0
  • 3.0.0
  • 3.0.0
Titanium.UI.iOS.CollisionBehavior
: Number
Specifies the collision behavior. ...

Specifies the collision behavior.

This API can be assigned the following constants:

Default: Titanium.UI.iOS.COLLISION_MODE_ALL

Titanium.UI.iOS.CollisionBehavior
items : Titanium.UI.View[]readonly

Items added to this behavior.

Items added to this behavior.

The Window or TabGroup whose Activity lifecycle should be triggered on the proxy.

The Window or TabGroup whose Activity lifecycle should be triggered on the proxy.

If this property is set to a Window or TabGroup, then the corresponding Activity lifecycle event callbacks will also be called on the proxy. Proxies that require the activity lifecycle will need this property set to the appropriate containing Window or TabGroup.

  • 3.6.0
Titanium.UI.iOS.CollisionBehavior
: ReferenceInsets
Insets to apply when using the animator's reference view as the boundary. ...

Insets to apply when using the animator's reference view as the boundary.

The treatReferenceAsBoundary property needs to be set to true to use this property.

Default: All insets are zero.

Titanium.UI.iOS.CollisionBehavior
: Boolean
Use the animator's reference view as the boundary. ...

Use the animator's reference view as the boundary.

Set to true to enable this behavior or false to disable it.

Default: true

Defined By

Methods

Titanium.UI.iOS.CollisionBehavior
( boundary )
Adds a boundary to this behavior. ...

Adds a boundary to this behavior.

Parameters

Returns

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

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

Parameters

  • name : String

    Name of the event.

  • callback : Callback<Object>

    Callback function to invoke when the event is fired.

Returns

  • void
Titanium.UI.iOS.CollisionBehavior
( item )
Adds an item to this behavior. ...

Adds an item to this behavior.

Parameters

Returns

  • void
Applies the properties to the proxy. ...

Applies the properties to the proxy.

Properties are supplied as a dictionary. Each key-value pair in the object is applied to the proxy such that myproxy[key] = value.

  • 3.0.0
  • 3.0.0
  • 3.0.0

Parameters

  • props : Dictionary

    A dictionary of properties to apply.

Returns

  • void
Fires a synthesized event to any registered listeners. ...

Fires a synthesized event to any registered listeners.

Parameters

  • name : String

    Name of the event.

  • event : Dictionary

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

Returns

  • void
Gets the value of the apiName property. ...

Gets the value of the apiName property.

  • 3.2.0
  • 3.2.0
  • 3.2.0

Returns

  • String
Titanium.UI.iOS.CollisionBehavior
( ) : BoundaryIdentifier[]
Gets the value of the boundaryIdentifiers property. ...

Gets the value of the boundaryIdentifiers property.

Returns

Gets the value of the bubbleParent property. ...

Gets the value of the bubbleParent property.

  • 3.0.0
  • 3.0.0
  • 3.0.0

Returns

  • Boolean
Titanium.UI.iOS.CollisionBehavior
( ) : Number
Gets the value of the collisionMode property. ...

Gets the value of the collisionMode property.

Returns

  • Number
Titanium.UI.iOS.CollisionBehavior
( ) : Titanium.UI.View[]
Gets the value of the items property. ...

Gets the value of the items property.

Returns

Gets the value of the lifecycleContainer property. ...

Gets the value of the lifecycleContainer property.

  • 3.6.0

Returns

Titanium.UI.iOS.CollisionBehavior
( ) : ReferenceInsets
Gets the value of the referenceInsets property. ...

Gets the value of the referenceInsets property.

Returns

Titanium.UI.iOS.CollisionBehavior
( ) : Boolean
Gets the value of the treatReferenceAsBoundary property. ...

Gets the value of the treatReferenceAsBoundary property.

Returns

  • Boolean
Titanium.UI.iOS.CollisionBehavior
( )
Removes all boundaries from this behavior. ...

Removes all boundaries from this behavior.

Returns

  • void
Titanium.UI.iOS.CollisionBehavior
( boundary )
Removes the specified boundary from this behavior. ...

Removes the specified boundary from this behavior.

Parameters

Returns

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

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

Multiple listeners can be registered for the same event, so the callback parameter is used to determine which listener to remove.

When adding a listener, you must save a reference to the callback function in order to remove the listener later:

var listener = function() { Ti.API.info("Event listener called."); }
window.addEventListener('click', listener);

To remove the listener, pass in a reference to the callback function:

window.removeEventListener('click', listener);

Parameters

  • name : String

    Name of the event.

  • callback : Callback<Object>

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

Returns

  • void
Titanium.UI.iOS.CollisionBehavior
( item )
Removes the specified item from this behavior. ...

Removes the specified item from this behavior.

Parameters

Returns

  • void
Sets the value of the bubbleParent property. ...

Sets the value of the bubbleParent property.

  • 3.0.0
  • 3.0.0
  • 3.0.0

Parameters

  • bubbleParent : Boolean

    New value for the property.

Returns

  • void
Titanium.UI.iOS.CollisionBehavior
( collisionMode )
Sets the value of the collisionMode property. ...

Sets the value of the collisionMode property.

Parameters

  • collisionMode : Number

    New value for the property.

Returns

  • void
Sets the value of the lifecycleContainer property. ...

Sets the value of the lifecycleContainer property.

  • 3.6.0

Parameters

Returns

  • void
Titanium.UI.iOS.CollisionBehavior
( referenceInsets )
Sets the value of the referenceInsets property. ...

Sets the value of the referenceInsets property.

Parameters

Returns

  • void
Titanium.UI.iOS.CollisionBehavior
( treatReferenceAsBoundary )
Sets the value of the treatReferenceAsBoundary property. ...

Sets the value of the treatReferenceAsBoundary property.

Parameters

  • treatReferenceAsBoundary : Boolean

    New value for the property.

Returns

  • void
Defined By

Events

Titanium.UI.iOS.CollisionBehavior
Fired when an item collides with a boundary. ...

Fired when an item collides with a boundary.

Properties

  • item : Titanium.UI.View

    Item that collided with the boundary.

  • identifier : String

    Identifier of the boundary the item collided with.

  • point : Point

    Point of the collision when it started. Only returned when start is true.

  • start : Boolean

    Returns true if the collision started else false.

  • source : Object

    Source object that fired the event.

    •  
    •  
    •  
  • type : String

    Name of the event fired.

    •  
    •  
    •  
  • bubbles : Boolean

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

    •  
    •  
    •  
  • cancelBubble : Boolean

    Set to true to stop the event from bubbling.

    •  
    •  
    •  
Titanium.UI.iOS.CollisionBehavior
Fired when two items collide. ...

Fired when two items collide.

Properties

  • item1 : Titanium.UI.View

    Item that collided with item2.

  • item2 : Titanium.UI.View

    Item that collided with item1.

  • point : Point

    Point of the collision when it started. Only returned when start is true.

  • start : Boolean

    Returns true if the collision started else false.

  • source : Object

    Source object that fired the event.

    •  
    •  
    •  
  • type : String

    Name of the event fired.

    •  
    •  
    •  
  • bubbles : Boolean

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

    •  
    •  
    •  
  • cancelBubble : Boolean

    Set to true to stop the event from bubbling.

    •  
    •  
    •