UIViewControllerTransitioningDelegate method not called in iOS 7

前端 未结 3 915
遥遥无期
遥遥无期 2020-12-18 09:24

I have created a custom transition animation for a modal view controller by implementing the methods in the UIViewControllerTransitioningDelegate protocol.

相关标签:
3条回答
  • 2020-12-18 09:57

    Setting the transitioningDelegate in the presenting view controller's initialiser instead of the viewDidLoad method fixed the issue.

    It appears in iOS 7 a view controller's transitioning logic is called before viewDidLoad, but is the other way around from iOS 8 onwards.

    0 讨论(0)
  • 2020-12-18 10:01

    If anyone comes across this in later years, and you're using Swift 3, make sure that your call isn't to "animationControllerForPresentedController".

    As of Xcode 8.1, the compiler doesn't automatically recognize this problem and thus doesn't offer to convert the code to modern syntax.

    The above code will compile but it won't be a protocol implementation. It will just be a custom, uncalled function. (Such are the hazards of optional protocol methods, and the myriad problems with Xcode's autocomplete.)

    So, make sure you implement the protocol with Swift 3 syntax:

    func animationController(forPresented presented: UIViewController,
            presenting: UIViewController,
            source: UIViewController)
            -> UIViewControllerAnimatedTransitioning?
    {
        // ... return your cached controller object here.
    }
    

    And remember to set the presentation style on the View Controller to be presented:

    self.modalPresentationStyle = .custom
    

    And the delegate:

    self.transitioningDelegate = self // or wherever
    
    0 讨论(0)
  • 2020-12-18 10:03

    Another issue is that transitioningDelegate is a weak property. So you can assign to it, then have your transitioning delegate class be released before the transition has a chance to run. When the transition does run, the value of transitioningDelegate is nil, and your methods never get called.

    To see this, do the following:

    let myVC = UIViewController(nibName: nil, bundle: nil)
    likesVC.transitioningDelegate = BackgroundFadesInPresentationDelegate(viewController: likesVC)
    likesVC.modalPresentationStyle = .custom
    present(likesVC, animated: true, completion: nil)
    

    Then in your transition delegate class, add

    deinit {
        print("deinit")
    }
    

    And see if that print statement is hit before the transition.

    You will run into this problem if you use a freestanding class to implement the UIViewControllerTransitioningDelegate. This is why tutorials such as this one generally have you implement the transitioning delegate in the either in the view controller class itself or as an extension. Other things are keeping the view controller from getting released.

    In general in Cocoa Touch anything named "...Delegate" will be a weak property, to help avoid retain cycles. You should make your own custom class delegate properties weak as well. There's a good section on this in Apple's Cocoa Core Competencies.

    0 讨论(0)
提交回复
热议问题