gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 05:40:10 25 4
gpt4 key购买 nike

我有一个非常简单的动画 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com