UIPercentDrivenInteractiveTransition doesn't get to animation's completion on fast gesture

前端 未结 3 1462
礼貌的吻别
礼貌的吻别 2021-02-04 19:05

I have created an interactive transition. My func animateTransition(transitionContext: UIViewControllerContextTransitioning) is quite normal, I get the container

3条回答
  •  悲哀的现实
    2021-02-04 19:31

    When I was diagnosing this problem, I noticed that the gesture's change and ended state events were taking place before animateTransition even ran. So the animation was canceled/finished before it even started!

    I tried using GCD animation synchronization queue to ensure that the updating of the UIPercentDrivenInterativeTransition doesn't happen until after `animate:

    private let animationSynchronizationQueue = dispatch_queue_create("com.domain.app.animationsynchronization", DISPATCH_QUEUE_SERIAL)
    

    I then had a utility method to use this queue:

    func dispatchToMainFromSynchronizationQueue(block: dispatch_block_t) {
        dispatch_async(animationSynchronizationQueue) {
            dispatch_sync(dispatch_get_main_queue(), block)
        }
    }
    

    And then my gesture handler made sure that changes and ended states were routed through that queue:

    func handlePan(gesture: UIPanGestureRecognizer) {
        switch gesture.state {
    
        case .Began:
            dispatch_suspend(animationSynchronizationQueue)
            fromViewController.performSegueWithIdentifier("segueID", sender: gesture)
    
        case .Changed:
            dispatchToMainFromSynchronizationQueue() {
                self.updateInteractiveTransition(percentage)
            }
    
        case .Ended:
            dispatchToMainFromSynchronizationQueue() {
                if isOkToFinish {
                    self.finishInteractiveTransition()
                } else {
                    self.cancelInteractiveTransition()
                }
            }
    
        default:
            break
        }
    }
    

    So, I have the gesture recognizer's .Began state suspend that queue, and I have the animation controller resume that queue in animationTransition (ensuring that the queue starts again only after that method runs before the gesture proceeds to try to update the UIPercentDrivenInteractiveTransition object.

提交回复
热议问题