Migrating from `POPSpringAnimation` to native iOS framework in Swift 4

I am working on an old project and want to get rid of the POP framework. I'm sure any animation can be done with the native iOS framework.

Here's the old code:

POPSpringAnimation *springAnimation = [POPSpringAnimation animationWithPropertyNamed:kPOPViewFrame];
springAnimation.toValue = [NSValue valueWithCGRect:rect];
springAnimation.velocity = [NSValue valueWithCGRect:CGRectMake(springVelocity, springVelocity, 0, 0)];
springAnimation.springBounciness = springBounciness;
springAnimation.springSpeed = springSpeed;
[springAnimation setCompletionBlock:^(POPAnimation *anim, BOOL finished) {
    if (finished) {
         // cool code here
    }
}];

[self.selectedViewController.view pop_addAnimation:springAnimation forKey:@"springAnimation"];

      

What I have tried:

[UIView animateWithDuration:1.0
                      delay:0
     usingSpringWithDamping:springBounciness
      initialSpringVelocity:springVelocity
                    options:UIViewAnimationOptionCurveEaseInOut animations:^{
                        self.selectedViewController.view.frame = rect;
} completion:^(BOOL finished) {
    // cool code here
}];

      

But I am not getting the same result and the question arises:

  • springBounciness

    in pop equivalent usingSpringWithDamping

    ?
  • What's the equivalent springSpeed

    in animation UIView

    ?
  • How about the duration, what is the duration POPSpringAnimation

    ?

Edit: About the third question, I found an issue on Github.

If UIView

not appropriate, can this be done with Core Animation or any other native iOS animation environment?

+3


source to share


1 answer


Pop values ​​range from 0-20. But useSpringWithDamping doesn't have that range. Obviously Pop is a regular library, it has its own range of values, and the UIView animation has its own.

From Apple's documentation, using the SpringWithDamping parameter is indeed a wet relationship and it states:

To smoothly slow down the animation without wobbling, use a value of 1. Use a damping factor closer to zero to increase wobble.

1. If you want an equivalent expectation, you need to use values ​​below 1, I think you could try the following formula for springBounciness.

float uiViewBounciness = (20.0 - springBounciness) / 20.0;
.. usingSpringWithDamping:uiViewBounciness ..

      



2. Regarding springVelocity, Pop implements the same speed for all animation frames, whereas UIView animation only defines the INITIAL speed, and this speed decays over time depending on the total duration and humidity. To get the animation as close as possible, you can do the following:

float uiViewSpeed = springVelocity * 2.0; 
.. initialSpringVelocity:uiViewSpeed  ..

      

3. For duration, you can implement the same value for animateWithDuration in the UIView method.

Finally, you need to experiment with the values ​​and compare them to the Pop animation. I don't think you can get accurate Pop animations using UIView animations, but it should be close enough.

+1


source







All Articles