You are reading 13.0.1. See this page in 13.4.1.
Titanium.UI.iOS.CollisionBehavior
Extends Titanium.Proxy
Dynamic behavior to support collisions between items and boundaries.
A collision behavior specifies the behavior when items collide with each other and boundaries. To define a collision behavior:
- Use the Titanium.UI.iOS.createCollisionBehavior method to create and define the behavior.
- Use the addItem method to add items to the behavior.
- 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.
- 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();
Properties 7
Inherited from Titanium.Proxy
The name of the API that this proxy corresponds to.
Boundary identfiers added to this behavior.
Inherited from Titanium.Proxy
Indicates if the proxy will bubble an event to its parent.
Default: true
collisionMode
NumberSpecifies the collision behavior.
Default: <Titanium.UI.iOS.COLLISION_MODE_ALL>
Items added to this behavior.
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.
treatReferenceAsBoundary
BooleanUse the animator's reference view as the boundary.
Set to true to enable this behavior or false to disable it.
Default: true
Methods 9
Adds a boundary to this behavior.
Inherited from Titanium.Proxy
Adds the specified callback as an event listener for the named event.
| Parameters | Type | Description |
|---|---|---|
| name | String | Name of the event. |
| callback | Callback<Titanium.Event> | Callback function to invoke when the event is fired. 4 fields
|
Adds an item to this behavior.
| Parameters | Type | Description |
|---|---|---|
| item | Titanium.UI.View | View object to add to the behavior. |
applyProperties
since 3.0.0Inherited from Titanium.Proxy
Applies the properties to the proxy.
| Parameters | Type | Description |
|---|---|---|
| props | Dictionary | A dictionary of properties to apply. |
Inherited from Titanium.Proxy
Fires a synthesized event to any registered listeners.
| Parameters | Type | Description |
|---|---|---|
| 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. |
Removes all boundaries from this behavior.
Removes the specified boundary from this behavior.
Inherited from Titanium.Proxy
Removes the specified callback as an event listener for the named event.
| Parameters | Type | Description |
|---|---|---|
| name | String | Name of the event. |
| callback | Callback<Titanium.Event> | Callback function to remove. Must be the same function passed to 4 fields
|
Removes the specified item from this behavior.
| Parameters | Type | Description |
|---|---|---|
| item | Titanium.UI.View | Item to remove. |
Events 2
Fired when an item collides with a boundary.
| Event properties | Type | Description |
|---|---|---|
| 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 | Boolean | Returns |
Fired when two items collide.
| Event properties | Type | Description |
|---|---|---|
| item1 | Titanium.UI.View | Item that collided with |
| item2 | Titanium.UI.View | Item that collided with |
| point | Point | Point of the collision when it started. Only returned when |
| start | Boolean | Returns |