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 whil
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.
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];
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.
Inspired by this answer, I have a solution: update the transform on every frame of the animation
let attachment = UIAttachmentBehavior(item: item, attachedTo: item) // Workaround - attach the item to itself
attachment?.action = { () in
item.transform = item.transform.scaledBy(x: 1.5, y: 1.5)
}
animator.addBehavior(attachment)