Yuchen
Yuchen

Reputation: 33036

Cancel UIview.animation with user interaction

We have a horizontal animation like such:

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

We are doing so that the entire view content moved to the left a little bit (repeating twice) as an indication to the users that they can interact with our horizontal carousel (built with collection view).

However though, is there a way to cancel this animation if the user has interacted with the view?

There is a similar question asked: UIView animations canceling any touch input?. But it is not the same. What we are interested in is not only allowing user interaction but also cancel the animation immediately since we know that the user understands the UI already.

Upvotes: 2

Views: 629

Answers (1)

E.Coms
E.Coms

Reputation: 11531

If just cancel animation, just terminate all animations like this:

     self.collectionView.layer.removeAllAnimations()

It's not pausing but to run completion block.

Upvotes: 2

Related Questions