在下面的代碼中,我試圖在導航轉換期間創建自定義動畫,但導航控制器委託方法沒有收到呼叫。請看下面的代碼,並給我一個解決方案。導航控制器委託方法沒有收到呼叫
請注意,我已將DemoTransitionAnimationViewController嵌入到導航控制器中。該VC在其視圖上有一個按鈕。在點擊這個視圖時,我推着另一個視圖控制器。但代理方法仍然沒有接到電話。
CustomAnimator.swift
//
// CustomAnimator.swift
// LoginModule
//
// Created by Shubham Ojha on 8/14/17.
// Copyright © 2017 BBI. All rights reserved.
//
class FadeInAnimator: NSObject,
UIViewControllerAnimatedTransitioning {
func transitionDuration(
using transitionContext: UIViewControllerContextTransitioning?
) -> TimeInterval {
return 0.35
}
func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
let containerView = transitionContext.containerView
let fromVC = transitionContext.viewController(
forKey: UITransitionContextViewControllerKey.from)
let toVC = transitionContext.viewController(
forKey: UITransitionContextViewControllerKey.to)
containerView.addSubview(toVC!.view)
toVC!.view.alpha = 0.0
let duration = transitionDuration(using: transitionContext)
UIView.animate(withDuration: duration, animations: {
toVC!.view.alpha = 1.0
toVC?.view.backgroundColor = UIColor.blue
}, completion: { finished in
let cancelled = transitionContext.transitionWasCancelled
transitionContext.completeTransition(!cancelled)
})
}
}
class NavigationControllerDelegate: NSObject,
UINavigationControllerDelegate {
func navigationController(
_ navigationController: UINavigationController,
animationControllerFor operation:
UINavigationControllerOperation,
from fromVC: UIViewController,
to toVC: UIViewController
) -> UIViewControllerAnimatedTransitioning? {
return FadeInAnimator()
}
}
DemoTransitionAnimationViewController.swift
//
// DemoTransitionAnimationViewController.swift
// LoginModule
//
// Created by Shubham Ojha on 8/15/17.
// Copyright © 2017 BBI. All rights reserved.
//
import UIKit
class DemoTransitionAnimationViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
print(self.navigationController ?? "Not exist")
if self.navigationController != nil{
self.navigationController?.delegate = NavigationControllerDelegate()
// In the above statement if I am setting the delegate as self instead of
//NavigationControllerDelegate() and conforming the methods of navigation
//controller delegate protocol. It works perfectly.
}
else{
print("navigation controller does not exist")
}
}
}
嘗試將從NSObject繼承的類NavigationControllerDelegate更改爲UINavigationController。 –
我試過了,但還沒有接到電話。 Krunal的答案已經解決了我的問題,但是我想知道爲什麼我沒有在當前的代碼中調用?我正在更新我的問題。 –
因爲委託是指我們希望功能被執行的類,而不是根據我的理解分配新類。 –