通过用户交互取消UIview.animation

时间:2018-11-22 17:53:45

标签: ios

我们有一个类似这样的水平动画:

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)
})

我们这样做是为了使整个视图内容向左移一点(重复两次),以向用户指示他们可以与我们的水平旋转木马(使用收藏夹视图构建)进行交互。

但是,如果用户与视图进行了交互,是否可以取消此动画?

有一个类似的问题问:UIView animations canceling any touch input?。但这不一样。我们感兴趣的不仅是允许用户交互,而且因为我们已经知道用户已经理解UI,所以可以立即取消动画。

1 个答案:

答案 0 :(得分:1)

如果只是取消动画,则只需终止所有动画,如下所示:

     self.collectionView.layer.removeAllAnimations()

这不是暂停,而是运行完成块。