重复播放时 iOS 动画不会重置

标签 ios animation uiview

我有一个非常简单的动画 block ,它执行以下动画:

enter image description here
(tap the image if the animation doesn't play)

func didTapButton() {
  // reset the animation to 0
  centerYConstraint.constant = 0
  superview!.layoutIfNeeded()

  UIView.animate(
    withDuration: 1,
    animations: {
      // animate the view downwards 30 points
      self.centerYConstraint.constant = 30
      self.superview!.layoutIfNeeded()
  })
}

当我自己播放动画时,一切都很棒。它重置到位置 0,然后设置 30 个点的动画。

问题是当用户快速多次点击按钮时(即在正在进行的动画中间)。每次用户点击按钮时,我希望它重置到位置 0,然后向下移动 30 点。相反,我得到了这种行为:

enter image description here
(tap the image if the animation doesn't play)

显然它的行程超过了 30 个点。 (接近 120 分。)

为什么会这样,我怎样才能正确地“重置”动画,使其最多只移动 30 点?

我尝试过但不起作用的方法:

  • 使用选项:UIViewAnimationOptions.beginFromCurrentState。它执行完全相同的行为。
  • 不是直接执行动画,而是在几毫秒后使用 dispatch_after 执行.相同的行为。
  • "Canceling" the previous animation通过使用 0 第二个动画将常量更改为 0,并调用 superview!.layoutIfNeeded

其他说明:

  • 如果我不改变位置,而是通过约束改变高度,我会得到类似的奇怪行为。例如。如果我将它从 30 点设置为 15 点,当反复按下按钮时, View 显然会增长到 120 点左右。
  • 即使我不使用约束,而是将 transformCGAffineTransform.identity 动画化为 CGAffineTransform(scaleX: 0.5, y: 0.5) ,我得到相同的行为,它会增长到 120 点。
  • 如果我尝试一个完全不同的动画属性,比如 backgroundColor = UIColor(white: 0, alpha: 0.5)backgroundColor = UIColor(white: 0, alpha: 0),然后我得到正确的行为(即每次我点击按钮时,颜色最多重置为 0.5 灰色。它永远不会变黑。

最佳答案

我可以重现您描述的行为。但是,如果我在正在移动的 View 层上调用 removeAllAnimations(),问题就会消失。

@IBAction func didTapButton(_ sender: Any) {
    animatedView.layer.removeAllAnimations()

    centerYConstraint.constant = 0
    view.layoutIfNeeded()

    centerYConstraint.constant = 30
    UIView.animate(withDuration: 2) {
        self.view.layoutIfNeeded()
    }
}

请注意,我不是从父 View 中删除动画(因为它仍然表现出您描述的行为),而是从正在移动的 View 中删除动画。

关于重复播放时 iOS 动画不会重置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44150815/

相关文章:

ios - 如何在 WatchKit 应用程序中正确测试通知?

ios - UITableViewController 上的后退按钮编译错误

jquery - 滑动框过渡效果

ios - 添加延迟动画时,UIView 动画不起作用

ios - UIImageView 没有在 iPhone 中以正确的方向呈现图像

ios - 如何解决 xcode 中找不到 header 的问题

ios - Swift 动画问题

使用 block 动画方法的 iPhone 翻转 View

objective-c - 具有通用 UIView 的子类 UILabel 和 UITextView

ios - 如何让前导空间成为 SuperView 宽度的 1/3