UIKit Dynamics with existing affine transform

后端 未结 4 659
我在风中等你
我在风中等你 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:28

    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.

    0 讨论(0)
  • 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 <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];
    
    0 讨论(0)
  • 2021-02-14 19:35

    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.

    0 讨论(0)
  • 2021-02-14 19:40

    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)
    
    0 讨论(0)
提交回复
热议问题