Migrating from `POPSpringAnimation` to native iOS framework in Swift 4
Asked Answered
E

1

6

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

Here is 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 dont get the same result, and some question rises:

  1. is springBounciness in pop equivalent to usingSpringWithDamping ?
  2. What is equivalent of springSpeed in UIView animation ?
  3. what about the duration, what is the duration of POPSpringAnimation ?

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

If UIView is not the way to go can that be done using Core Animation or any other iOS native animation framework ?

Endarch answered 5/8, 2017 at 20:29 Comment(3)
Pop is using special solvers to decide on dampening. You might get closer with CASpringAnimation but Pop is hard to beat. Especially with interruptibility.Mazurek
Sorry to comment again but the solver solves for duration in this case. Yes to question 1 as well. I think 2 is a combo of dampening and initial velocity. 3. The solver uses dampening and view size along with the spring bounciness to solve for duration.Mazurek
@iosgeek pls check my answer. ty.Juvenescence
J
5

Pop parameter values range from 0-20. But the usingSpringWithDamping do not have such range. Obviously as Pop is a custom library, it has its own range of values while UIView animation has its own.

From Apple documentation, usingSpringWithDamping parameter is actually damp ratio, and it specifies:

To smoothly decelerate the animation without oscillation, use a value of 1. Employ a damping ratio closer to zero to increase oscillation.

1.So if you want equivalent bounciness, you need to use values anything below 1, I guess you could try the following formula for springBounciness.

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

2.As for springVelocity, Pop implements a same speed for all animation frames, whereas UIView animation only specifies the INITIAL speed, and this speed is decayed over time based on total duration and damp ratio. So to get as close animation as possible, you could do the following:

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

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

Finally, you need to experiment with the values and compare it to Pop animations. I don't think you can get the exact same animations with Pop by using UIView animate, but it should be close enough.

Juvenescence answered 14/8, 2017 at 2:16 Comment(1)
Fair enough ;) that's the only thing that we can do anyway.Endarch

© 2022 - 2024 — McMap. All rights reserved.