在 Swift 4 中从“POPSpringAnimation”迁移到本机 iOS 框架

iOS*_*eek 6 core-animation objective-c uiviewanimation ios facebook-pop

我正在开发一个旧项目,想要摆脱POP 框架,我确信任何动画都可以使用本机 iOS 框架来完成。

这是旧代码:

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"];
Run Code Online (Sandbox Code Playgroud)

我尝试过的:

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

但我没有得到相同的结果,并且出现了一些问题:

  1. springBounciness在流行音乐中相当于usingSpringWithDamping
  2. springSpeed动画中相当于什么UIView
  3. 持续时间怎么样? 的持续时间是多少POPSpringAnimation

编辑:关于第三个问题我发现了一个问题

如果UIView这不是可行的方法,可以使用 Core Animation 或任何其他 iOS 原生动画框架来完成吗?

Gen*_*ode 5

Pop 参数值范围为 0-20。但usingSpringWithDamping没有这样的范围。显然,由于 Pop 是一个自定义库,它有自己的值范围,而 UIView 动画也有自己的值范围。

从Apple文档来看,usingSpringWithDamping参数实际上是阻尼比,它指定:

要平滑地减速动画而不产生振荡,请使用值 1。采用接近于零的阻尼比来增加振荡。

1.所以如果你想要同等的弹力,你需要使用低于1的值,我想你可以尝试下面的 springBounciness 公式。

float uiViewBounciness = (20.0 - springBounciness) / 20.0;
.. usingSpringWithDamping:uiViewBounciness ..
Run Code Online (Sandbox Code Playgroud)

2.对于springVelocity,Pop对所有动画帧实现相同的速度,而UIView动画仅指定INITIAL速度,并且该速度根据总持续时间和阻尼比随时间衰减。因此,为了获得尽可能接近的动画,您可以执行以下操作:

float uiViewSpeed = springVelocity * 2.0; 
.. initialSpringVelocity:uiViewSpeed  ..
Run Code Online (Sandbox Code Playgroud)

3.对于duration,您可以在UIView方法中为animateWithDuration实现相同的值。

最后,您需要试验这些值并将其与 Pop 动画进行比较。我认为您无法通过使用 UIView animate 获得与 Pop 完全相同的动画,但它应该足够接近。