Canceling interactive UINavigationController pop gesture does not call UINavigationControllerDelegate methods

前端 未结 3 1362
忘掉有多难
忘掉有多难 2021-02-01 05:04

If you drag the edge of a UIViewController to begin an interactive pop transition within a UINavigationController, the UIViewController un

3条回答
  •  北海茫月
    2021-02-01 05:13

    I translated @Dima's answer to Swift for my project:

    func navigationController(navigationController: UINavigationController, willShowViewController viewController: UIViewController, animated: Bool) {
    
        transitionCoordinator()?.notifyWhenInteractionEndsUsingBlock { context in
            guard context.isCancelled(), let fromViewController = context.viewControllerForKey(UITransitionContextFromViewControllerKey) else { return }
    
            self.navigationController(self, willShowViewController: fromViewController, animated: animated)
    
            let animationCompletion: NSTimeInterval = context.transitionDuration() * Double(context.percentComplete())
    
            let delayTime = dispatch_time(DISPATCH_TIME_NOW, Int64(1 * Double(NSEC_PER_SEC)))
            dispatch_after(delayTime, dispatch_get_main_queue()) {
                self.navigationController(self, didShowViewController: fromViewController, animated: animated)
            }            
        }
    
        /* Your normal behavior goes here */
    
    }
    

    Note that I don't check for the existence of an implementation of navigationController(_:didShowViewController:animated:), although I believe this is checked at compile-time in Swift, and that you'll get a compiler error if you attempt to call this when it's unimplemented.

提交回复
热议问题