我们有一个像这样的水平动画:
UIView.animate(
withDuration: 400.millisecond,
delay: 600.milliseconds,
options: [.autoreverse, .beginFromCurrentState, .allowUserInteraction],
animations: {
UIView.setAnimationRepeatCount(5)
self.collectionView.transform = CGAffineTransform(a: 1, b: 0, c: 0, d: 1, tx: -30, ty: 0)
}, completion: { _ in
self.collectionView.transform = CGAffineTransform(a: 1, b: 0, c: 0, d: 1, tx: 0, ty: 0)
})
我们这样做是为了让整个 View 内容向左移动一点(重复两次),以向用户表明他们可以与我们的水平轮播(使用 Collection View 构建)进行交互。
但是,如果用户与 View 交互,有没有办法取消这个动画?
有一个类似的问题:UIView animations canceling any touch input? 。但它不一样。我们感兴趣的不仅是允许用户交互,而且还立即取消动画,因为我们知道用户已经理解了 UI。
最佳答案
如果只是取消动画,只需终止所有动画,如下所示:
self.collectionView.layer.removeAllAnimations()
这不是暂停,而是运行完成 block 。
关于ios - 取消 UIview.animation 与用户交互,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53436131/