Search code examples
iosswiftcore-animation

ios - CATransaction completion called multiple times


when i use CATransaction completion to wait animation finish, it called the completion many times. which is it should called just once. here is the sample code

@objc func spinWheel(sender: UIPanGestureRecognizer) {
    CATransaction.begin()
    CATransaction.setCompletionBlock{ [weak self] in
        print("hello")
    }
    startRotation(angle: calculatePosition)
    CATransaction.commit()
}

so after i swipe an UIView, it will rotate. and after finishing rotate i will try to print. and it will print it about 6-7 times. which should be called just once


Solution

  • I think your approach for this animation is wrong. When you add a pan gesture to a view and pan it. It keeps calling the delegate as long as you are panning.

    So what you should be doing is either replace the UIPanGestureRecognizer with UISwipeGestureRecognizer or handle the completion when pan gesture ends.