# Titanium.UI.iOS.DynamicItemBehavior

> Base dynamic configuration for an item.

- Source: https://titaniumsdk.com/docs/sdk/13.3.1/Titanium.UI.iOS.DynamicItemBehavior
- Titanium SDK version: 13.3.1
- Kind: proxy
- Platforms: iPhone, iPad, macOS
- Since: Titanium SDK iPhone 3.2.0, iPad 3.2.0, macOS 9.2.0
- Extends: Titanium.Proxy
- Inheritance: Titanium.Proxy

A dynamic item behavior configures the physics attributes for one or more items. These
attributes, such as density and resistance, affects the behavior of the object when other behaviors,
such as push forces or collisions, are applied to it.  To define a dynamic behavior for an item:

  1. Use the [Titanium.UI.iOS.createDynamicItemBehavior](https://titaniumsdk.com/docs/sdk/Titanium.UI.iOS.md#createDynamicItemBehavior) method to create and define the behavior.
  2. Use the [addItem](https://titaniumsdk.com/docs/sdk/Titanium.UI.iOS.DynamicItemBehavior.md#addItem) method to add items to the behavior.
  3. Add the behavior to an [Animator object](https://titaniumsdk.com/docs/sdk/Titanium.UI.iOS.Animator.md).

## Example: Simple Example

The following example create two blocks, which are pushed towards each other.  Because the
red block is more dense and has higher resistance than the blue block, the red block moves
steadily to the left, while the blue block spins around unpredictably.

<img src="./dynamicitem.gif" height="455" style="border:1px solid black"/>

``` js
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 red block
var redBlock = Ti.UI.createView({
    backgroundColor: 'red',
    width: 25,
    height: 25,
    top: 25,
    left: 25
});

// Change the physics attributes of the red block
var redDynamic = Ti.UI.iOS.createDynamicItemBehavior({
    density: 20.0,
    angularResistance: 1.0,
    friction: 1.0,
    resistance: 1.0,
    allowsRotation: false
});
redDynamic.addItem(redBlock);

// Apply a left push to the red block
var redPush = Ti.UI.iOS.createPushBehavior({
    pushDirection: {x: 2.0, y: 0.0}
});
redPush.addItem(redBlock);

// Create a blue block
var blueBlock = Ti.UI.createView({
    backgroundColor: 'blue',
    width: 50,
    height: 50,
    top: 25,
    right: 25
});

// Change the physics attributes of the blue block
var blueDynamic = Ti.UI.iOS.createDynamicItemBehavior({
    elasticity: 1.0,
});
blueDynamic.addItem(blueBlock);

// Apply a right push to the blue block
var bluePush = Ti.UI.iOS.createPushBehavior({
    pushDirection: {x: -2.0, y: 0.0}
});
bluePush.addItem(blueBlock);

// Create the collision behavior so the items can collide
var collision = Ti.UI.iOS.createCollisionBehavior();
collision.addItem(redBlock);
collision.addItem(blueBlock);

animator.addBehavior(redDynamic);
animator.addBehavior(redPush);
animator.addBehavior(blueDynamic);
animator.addBehavior(bluePush);
animator.addBehavior(collision);

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

win.add(redBlock);
win.add(blueBlock);
win.open();
```

## Properties

### allowsRotation

- Type: Boolean
- Platforms: iPhone, iPad, macOS
- Default: `true`

Specifies if this item can rotate.

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

### angularResistance

- Type: Number
- Platforms: iPhone, iPad, macOS
- Default: `0`

Specifies the angular resistance of this item.

The greater the value, the greater the angular damping and rotation slows to a stop faster.

### density

- Type: Number
- Platforms: iPhone, iPad, macOS
- Default: `1`

Specifies the relative mass density of this item.

An item's density along with its size determines its effective mass and affects its dynamic
behavior.

### elasticity

- Type: Number
- Platforms: iPhone, iPad, macOS
- Default: `0`

Specifies the elasticity applied to collisions for this item.

A value of 0.0 indicates no bounce upon collision, while 1.0 indicates a completely elastic
collision.

### friction

- Type: Number
- Platforms: iPhone, iPad, macOS
- Default: `0`

Specifies the linear resistance of the item when it slides against another item.

A value of 0.0 indicates no friction, while 1.0 indicates strong friction. Use higher
numbers to apply even higher friction.

### items

- Type: Array<Titanium.UI.View>
- Platforms: iPhone, iPad, macOS
- Permission: read-only

Items added to this behavior.

### resistance

- Type: Number
- Platforms: iPhone, iPad, macOS
- Default: `0`

Specifies the linear resistance of this item which reduces linear velocity over time.

A value of 0.0 indicates no velocity damping.

## Methods

### addAngularVelocityForItem

- Platforms: iPhone, iPad, macOS

Adds a specified angular velocity for the item.

Parameters:

- `item` (Titanium.UI.View): Item to add the velocity for.
- `velocity` (Number): Velocity to add or subtract in radians per second. If the current velocity is positive, the item spins clockwise. A negative value means the item spins counter-clockwise.

### addItem

- Platforms: iPhone, iPad, macOS

Adds an item to this behavior.

Parameters:

- `item` (Titanium.UI.View): View object to add to the behavior.

### addLinearVelocityForItem

- Platforms: iPhone, iPad, macOS

Adds a specified linear velocity for the item.

Parameters:

- `item` (Titanium.UI.View): Item to add the velocity for.
- `velocity` (Point): Velocity to add or substract in points per second in the x and y directions.

### angularVelocityForItem

- Platforms: iPhone, iPad, macOS

Returns the angular velocity of the item.

Parameters:

- `item` (Titanium.UI.View): Item to retrieve the velocity of.

Returns: Number

### linearVelocityForItem

- Platforms: iPhone, iPad, macOS

Returns the linear velocity of the item.

Parameters:

- `item` (Titanium.UI.View): Item to retrieve the velocity of.

Returns: Point

### removeItem

- Platforms: iPhone, iPad, macOS

Removes the specified item from this behavior.

Parameters:

- `item` (Titanium.UI.View): Item to remove.

## Inherited members

6 members are inherited. Each is documented on the type that declares it.

- [Titanium.Proxy](https://titaniumsdk.com/docs/sdk/13.3.1/Titanium.Proxy.md): `addEventListener`, `apiName`, `applyProperties`, `bubbleParent`, `fireEvent`, `removeEventListener`

---

- This corpus documents Titanium SDK 13.4.1. Anything here that does not name a version describes 13.4.1.
- Titanium is maintained by TiDev. The Appcelerator toolchain is retired: there is no `appc` CLI, no Appcelerator Studio and no hosted build service. The supported CLI is `titanium` on npm, invoked as `ti`.

