atalayasa
atalayasa

Reputation: 3480

CATransaction completion being called even view controller disappear

I am trying to animate my image view with three image sliding from left to right. I wrote following code to do it. However, setCompletionBlock is being called even when view controller disappear. So it causes weird problems with my previous view controller. How can I disable it in viewDidDisappear() or anywhere else?

func animateImageView() {
    CATransaction.begin()
    CATransaction.setAnimationDuration(OnboardAnimationConstants.animationDelayOnDidAppear)
    CATransaction.setCompletionBlock {
        DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
                self.animateImageView()
        }
    }
    transition.type = .push
    transition.subtype = .fromRight
    imageView.layer.add(transition, forKey: kCATransition)

    if images.count != 0 {
        imageView.image = images[index]
    }

    CATransaction.commit()
    index = index < images.count - 1 ? index + 1 : 0
}

Upvotes: 1

Views: 100

Answers (0)

Related Questions