【问题标题】:slowing down animation of UIDynamicAnimator减慢 UIDynamicAnimator 的动画
【发布时间】:2026-01-08 09:55:02
【问题描述】:

我想减慢我的 UIDynamicAnimator 生成的动画,以便我可以微调我的 UIDynamicBehaviors。

在 ios 模拟器中,Debug 菜单下有一个菜单选项,标签为“Toggle slow animations in frontmost app”。

但是,这个选项似乎对 UIDynamicAnimator 生成的动画没有影响。还有其他方法可以实现我的目标吗?

【问题讨论】:

  • 你不能对用于执行动画的动画块使用更长的持续时间吗(你是否使用 animateWithDuration:.... 方法之一)?
  • 不,我没有直接使用动画,因为 UIDynamicAnimator 为我做的。

标签: ios animation uidynamicbehavior uidynamicanimator


【解决方案1】:

您可以更改您在动画中指定的力以降低移动速度。例如,您可以更改重力大小以减慢加速度:

    self.animator = UIDynamicAnimator(referenceView: self.view)
    var gravity = UIGravityBehavior(items: [animatedView])
    gravity.magnitude = 0.5

如果发生碰撞,您还可能会增加摩擦力和/或弹性以减慢速度,但在某些情况下这可能会影响轨迹:

    let bounceProperties = UIDynamicItemBehavior(items: [animatedView])
    bounceProperties.elasticity = 0.5
    bounceProperties.friction = 0.2

    var collision = UICollisionBehavior(items: [animatedView])
    var boundaryId: NSMutableString = NSMutableString(string: "bottomBoundary")
    let boundaryPath = UIBezierPath(rect: boundaryFrame)
    collision.addBoundaryWithIdentifier(boundaryId, forPath: boundaryPath)

    // Start animating
    animator.addBehavior(gravity)
    animator.addBehavior(collision)
    animator.addBehavior(bounceProperties)

【讨论】:

    最近更新 更多