代码之家  ›  专栏  ›  技术社区  ›  iOSGeek

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

  •  5
  • iOSGeek  · 技术社区  · 8 年前

    我在做一个老项目,想摆脱 POP framework 我确信任何动画都可以用原生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
    }];
    

    1. springBounciness usingSpringWithDamping
    2. springSpeed 在里面 UIView
    3. POPSpringAnimation

    编辑: 关于第三个问题,我发现 issue 在Github中。

    如果 UIView视图 难道这不是使用核心动画或任何其他iOS原生动画框架可以做到的吗?

    1 回复  |  直到 8 年前
        1
  •  4
  •   GeneCode    8 年前

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

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

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

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

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

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

    推荐文章