9

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. :)

Alex Zak
  • 1,924
  • 2
  • 18
  • 26
  • Good question, I have been trying to hack this all evening :-) Please post back when you have findings. – Unheilig Oct 13 '13 at 00:01
  • 3
    I haven't run across this myself yet, but one potential strategy is to use an extra container view that you apply the UIKit Dynamics to, then the view you want to scale is a subview of that container view (centered within it) and now you should be able to apply whatever transforms you want onto the view without them interfering with UIKit Dynamics. – smileyborg Oct 13 '13 at 22:48

4 Answers4

15

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];
Scott Berrevoets
  • 16,921
  • 6
  • 59
  • 80
  • Tah, can't believe I missed that... Thanks mate! :) – Alex Zak Jan 08 '14 at 22:50
  • is this apply for `scale`? As the document mentioned the initial state(position and rotation), and as this [answer](http://stackoverflow.com/questions/20631383/uidynamicitem-update-transform-manually) said, "UIDynamicAnimator only supports rotation and position, not scale (or any other type of affine transform)." – funclosure Apr 18 '17 at 12:49
3

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.

mengxiangjian
  • 552
  • 2
  • 8
2

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.

Avario
  • 4,655
  • 3
  • 26
  • 19
0

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)
Max Chuquimia
  • 7,494
  • 2
  • 40
  • 59