如何在 UIScrollView.layer.addAnimation 中停止 CAKeyframeAnimation - removeAnimationForKey 和 removeAllAnimations 什么都不做

How to stop CAKeyframeAnimation in UIScrollView.layer.addAnimation - removeAnimationForKey and removeAllAnimations do nothing

我用这种方式制作了 UIScrollView 的动画:

let anim = CAKeyframeAnimation()
anim.keyPath = "bounds.origin.x"
anim.values = self.animValues
anim.keyTimes = self.animKeyTimes
anim.duration = self.animDuration
anim.additive = true
myScrollView.layer.addAnimation(anim, forKey: "move")

然后当用户点击取消时,我尝试了这个:

myScrollView.layer.removeAnimationForKey("move")
myScrollView.layer.removeAllAnimations()

两者都不行。 我也尝试设置 anim.duration = 0.0。那没有效果。 我不能尝试用 myScrollView.layer.presentationLayer() 做任何事情,因为 presentationLayer()nil.

动画不需要暂停。它们可以完全停止和丢弃。它们永远不会重新启动。

这些动画可以是 30 秒或更长时间。强迫用户等到它完成是不好的 UX/UI.

如有任何帮助,我们将不胜感激。

谢谢,

大卫

您可以尝试将动画速度设置为零

myScrollView.layer.speed = 0.0

尝试 anim.keyPath = "position.x" 而不是 "bounds.origin.x"。这对我有用 layer.removeAllAnimations()

我不得不为一个不同的问题重建我的项目文件,当我再次尝试时,它按预期的方式工作!

这至少是我第三次不得不为此应用重建项目文件。 我一定是做对了!