0

Im trying to understand how the UIDynamicAnimator calculates the values each iteration.

For example i have a simpel exampel where i have a UIAttachmentBehavior and a UIDynamicItemBehavior attached to the same DynamicItem

UIAttachmentBehavior *attachmentBehavior = [[UIAttachmentBehavior alloc] initWithItem:test attachedToAnchor:CGPointMake(50, 50)];
attachmentBehavior.frequency = 3.5;
attachmentBehavior.damping = .4;

UIDynamicItemBehavior *dynamicItemBehavior = [[UIDynamicItemBehavior alloc] initWithItems:@[test]];
dynamicItemBehavior.resistance = 10;
dynamicItemBehavior.action = ^{
    NSLog(@"%f", [dynamicItemBehavior linearVelocityForItem:test]);
};

Whenever i change the anchorPoint of the UIAttachmentBehavior it will animate to that location with a spring animation.

I dont understand how the DynamicItemBehavior knows about this movement, and thereby can apply its physics properties (resistance, density etc) to the movement triggered by a different Behavior?

Does the UIDynamicAnimator somehow prioritise and accumulate the values from all the behaviors (velocity, damping etc).

in my head it does something like

var velocity;

for behavior in item.behaviors
    velocity += behavior.velocity * behavior.damping    

item.position = delta_time * velocity
Mads Lee Jensen
  • 4,570
  • 5
  • 36
  • 53

1 Answers1

1

I'm not sure I entirely understand what you're asking, but I happened upon this while studying UIDynamics a couple days ago. Maybe it can help you out: http://www.raywenderlich.com/76147/uikit-dynamics-tutorial-swift

About halfway down the page he talks about how exactly the behaviors are being calculated behind the scenes:

The method signatures for the dynamic behaviors use the term items rather than views. The only requirement to apply dynamic behavior to an object is that it adopts the UIDynamicItem protocol, as so: protocol

UIDynamicItem : NSObjectProtocol {   
var center: CGPoint { get set }
var bounds: CGRect { get }
var transform: CGAffineTransform { get set } }

The UIDynamicItem protocol gives dynamics read and write access to the center and transform properties, allowing it to move the items based on its internal computations. It also has read access to bounds, which it uses to determine the size of the item. This allows it to create collision boundaries around the perimeter of the item as well as compute the item’s mass when forces are applied. This protocol means that dynamics is not tightly coupled to UIView; indeed there is another UIKit class that isn’t a view but still adopts this protocol: UICollectionViewLayoutAttributes. This allows dynamics to animate items within collection views.

There's more to it than what I've quoted here. It's a good read on the topic as a whole, I thought. Hope this helps!

Bryan Chapel
  • 346
  • 2
  • 12