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

在Swift 4中从“POPSpringAnimation”迁移到本机iOS框架,ios,objective-c,core-animation,uiviewanimation,facebook-pop,Ios,Objective C,Core Animation,Uiviewanimation,Facebook Pop,我正在做一个老项目,想摆脱我确信任何动画都可以用原生iOS框架完成 以下是旧代码: POPSpringAnimation *springAnimation = [POPSpringAnimation animationWithPropertyNamed:kPOPViewFrame]; springAnimation.toValue = [NSValue valueWithCGRect:rect]; springAnimation.velocity = [NSValue valueWithCGRe

我正在做一个老项目,想摆脱我确信任何动画都可以用原生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"];
我所尝试的:

[UIView animateWithDuration:1.0
                      delay:0
     usingSpringWithDamping:springBounciness
      initialSpringVelocity:springVelocity
                    options:UIViewAnimationOptionCurveEaseInOut animations:^{
                        self.selectedViewController.view.frame = rect;
} completion:^(BOOL finished) {
    // cool code here
}];
但我并没有得到同样的结果,一些问题出现了:

  • pop中的
    springbouncity
    是否等同于
    使用带阻尼的springwithdamping
  • UIView
    动画中,什么与
    springSpeed
    等效
  • 持续时间是多少?POPSpringAnimation的持续时间是多少 编辑: 关于我在Github中发现的第三个问题


    如果
    UIView
    不是可行的方法,那么可以使用核心动画或任何其他iOS本机动画框架来实现吗

    Pop参数值的范围为0-20。但是使用带阻尼的弹簧没有这样的范围。显然,由于Pop是一个自定义库,它有自己的值范围,而UIView动画有自己的值范围

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

    若要在不发生振荡的情况下平滑减速动画,请使用值 第1页。采用接近零的阻尼比来增加振荡

    1.所以,如果你想要等效的弹性,你需要使用小于1的值,我想你可以试试下面的弹性公式

    float uiViewBounciness = (20.0 - springBounciness) / 20.0;
    .. usingSpringWithDamping:uiViewBounciness ..
    
    2.对于springVelocity,Pop对所有动画帧实现相同的速度,而UIView动画仅指定初始速度,该速度根据总持续时间和阻尼比随时间衰减。因此,要获得尽可能接近的动画效果,可以执行以下操作:

    float uiViewSpeed = springVelocity * 2.0; 
    .. initialSpringVelocity:uiViewSpeed  ..
    
    3.对于duration,您可以在UIView方法中实现相同的值来设置animateWithDuration


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

    Pop使用特殊的解算器来决定阻尼。你可能会更接近CASpringAnimation,但Pop很难被打败。特别是在可中断性方面。很抱歉再次发表评论,但在这种情况下,解算器会解决持续时间问题。问题1也是。我认为2是阻尼和初始速度的组合。3.解算器使用阻尼和视图大小以及弹簧弹性来解算持续时间。@iosgeek请检查我的答案。很公平;)这是我们唯一能做的事。