UIKit Dynamics with existing affine transform

后端 未结 4 667
我在风中等你
我在风中等你 2021-02-14 19:07

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

4条回答
  •  攒了一身酷
    2021-02-14 19:31

    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  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];
    

提交回复
热议问题