Im folgenden Code versuche ich, einen benutzerdefinierten Animator während der Navigationsüberleitung zu erstellen, aber die Delegiertenmethode des Navigationscontrollers erhält keinen Anruf. Bitte schauen Sie sich den folgenden Code an und schlagen Sie mir eine Lösung vor.Die Delegiertenmethode des Navigationscontrollers erhält keinen Anruf
Bitte beachten Sie, dass ich den DemoTransitionAnimationViewController in einen Navigationscontroller eingebettet habe. Dieser VC hat eine Schaltfläche in seiner Ansicht. Beim Klicken auf diese Ansicht drehe ich einen anderen View-Controller. Aber immer noch erhält die Delegate-Methode keinen Anruf.
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")
}
}
}
Versuchen Sie, die von NSObject geerbte Klasse NavigationControllerDelegate in UINavigationController zu ändern. –
Ich habe es versucht, aber immer noch keinen Anruf bekommen. Die Antwort von Krunal hat mein Problem gelöst. Aber ich möchte wissen, warum ich im aktuellen Code keinen Anruf bekomme. Ich aktualisiere meine Frage. –
Da delegate sich auf die Klasse bezieht, in der die Funktionalität ausgeführt werden soll, anstatt eine neue Klasse zuzuweisen ... nach meinem Verständnis. –