real-world movement Goes beyond Core Animation with collisions and forces Can include Attachments to other objects or Points Can really add some polish to the “feel” of the UI.
access to the underlying physics engine We add attachments, collisions etc. though UIBehavior Classes The animator manipulates UIViews or other UIBehaviorItem types by modifying the Center or Transform properties The animator cycles through each iteration of movement until the objects come to rest
View which should also be the parent view of any items we want to animate. The animator continually calculates the impact of any active behaviors. ! UIDynamicAnimator *animator = [[UIDynamicAnimator alloc] initWithReferenceView:self.view];
animator can manipulate UIView & UICollectionViewLayoutAttributes adopt this protocol Defines only 3 Properties that we’re already very familiar with: • Bounds • Center • Transform We can adopt this in our classes to create custom Dynamic items
Declares the Action property that can hold a block that is called for each iteration of the animation loop. Declares the Child Behavior properties and methods
an anchor point or another item. Attachment points can be offset ! UIAttachmentBehavior *attachment = [[UIAttachmentBehavior alloc] initWithItem:self.square1 offsetFromCenter:attachmentPoint attachedToAnchor:centerPoint]; ! [animator addBehavior:attachmentBehavior];
[sender locationInView:[self.imageView superview]]; CGPoint offset = [sender locationInView:self.imageView]; self.imageOffset = UIOffsetMake(offset.x-self.imageView.bounds.size.width/2, offset.y-self.imageView.bounds.size.height/2); if (sender.state == UIGestureRecognizerStateBegan) { // The image offset and the position use different coordinates but must map to the // same point on the SquareView to get the effect we want self.attachmentBehavior = [[UIAttachmentBehavior alloc] initWithItem:self.imageView offsetFromCenter:self.imageOffset attachedToAnchor:position]; ! [self.animator addBehavior:self.attachmentBehavior]; } else if (sender.state == UIGestureRecognizerStateChanged) { // As the anchor point and the offset coincide the pan gesture appears to move the // imageView directly but this is actually handled by the animator. ! self.attachmentBehavior.anchorPoint = position; } else if (sender.state == UIGestureRecognizerStateEnded) {
It's important that the animator behavoirs are inactive or // removed by this point to avoid problems with Core Animation [UIView animateWithDuration:duration animations:^{ self.imageView.center = self.oldCenter; self.imageView.transform = CGAffineTransformIdentity; }]; }