Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIKit Dynamics with existing affine transform

I have a view that has some scale transformations. And when I apply some UIKit Dynamics on it, it zeroes them out. /: How can I keep the existing transformation on the view while having it jump around? :P

Thanks. :)

like image 306
Alex Zak Avatar asked Oct 12 '13 19:10

Alex Zak


3 Answers

I found the easiest way around this was to just apply the UIKitDynamics behaviours to a container view and apply the scaling/transforms to a subview within that container.

This way you can also animate the transform while still applying the dynamic behaviour.

like image 116
Avario Avatar answered Nov 15 '22 20:11

Avario


Take a look at UIDynamicAnimator's updateItemUsingCurrentState.

A dynamic animator automatically reads the initial state (position and rotation) of each dynamic item you add to it, and then takes responsibility for updating the item’s state. If you actively change the state of a dynamic item after you’ve added it to a dynamic animator, call this method to ask the animator to read and incorporate the new state.

So anytime you change the transform after the item you're transforming has been added to an animator, just call updateItemUsingCurrentState right after.

id <UIDynamicItem> dynamicItem; // whatever your item is, probably a UIView
UIGravityBehavior *behavior = [[UIGravityBehavior alloc] initWithItems:@[dynamicItem]];
UIDynamicAnimator *animator = [[UIDynamicAnimator alloc] initWithReferenceView:self.view]; // or however you're getting your animator

[animator addBehavior:behavior];

view.transform = CGAffineTransformMakeScale(1.5, 1.5);
[animator updateItemUsingCurrentState:view];
like image 44
Scott Berrevoets Avatar answered Nov 15 '22 18:11

Scott Berrevoets


Here is a tutorial,http://www.raywenderlich.com/50197/uikit-dynamics-tutorial. The author said,we can’t use a transform to scale your object while it is under the control of dynamics. I hope the article could help you.

like image 45
mengxiangjian Avatar answered Nov 15 '22 20:11

mengxiangjian