[英]Interactive Delegate Methods Never Called
我想在ViewController(1)和NavigationViewController(2)之間進行交互式轉換。
NavigationController由一個按鈕調用,因此在呈現時沒有交互式轉換。 它可以通過按鈕或UIPanGestureRecognizer來解除,因此它可以被交互或不被解雇。
我有一個名為TransitionManager的對象,用於轉換,UIPercentDrivenInteractiveTransition的子類。
下面的代碼的問題是從不調用兩個委托方法interactionControllerFor...
此外,當我按下按鈕或swip(UIPanGestureRecognizer)時,模態segue的基本動畫完成。 所以兩個委托方法animationControllerFor...
也不起作用。
有任何想法嗎 ? 謝謝
let transitionManager = TransitionManager()
override func viewDidLoad() {
super.viewDidLoad()
self.transitioningDelegate = transitionManager
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
let dest = segue.destinationViewController as UIViewController
dest.transitioningDelegate = transitionManager
dest.modalPresentationStyle = .Custom
}
class TransitionPushManager: UIPercentDrivenInteractiveTransition,
UINavigationControllerDelegate, UIViewControllerTransitioningDelegate {
@IBOutlet var navigationController: UINavigationController!
var animation : Animator! // Implement UIViewControllerAnimatedTransitioning protocol
override func awakeFromNib() {
var panGesture = UIPanGestureRecognizer(target: self, action: "gestureHandler:")
navigationController.view.addGestureRecognizer(panGesture)
animation = Animator()
}
func gestureHandler(pan : UIPanGestureRecognizer) {
switch pan.state {
case .Began :
interactive = true
navigationController.presentingViewController?.dismissViewControllerAnimated(true, completion:nil)
case .Changed :
...
default :
...
interactive = false
}
}
func animationControllerForPresentedController(presented: UIViewController, presentingController presenting: UIViewController, sourceController source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
return animation
}
func animationControllerForDismissedController(dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
return animation
}
func interactionControllerForPresentation(animator: UIViewControllerAnimatedTransitioning) -> UIViewControllerInteractiveTransitioning? {
return nil
}
func interactionControllerForDismissal(animator: UIViewControllerAnimatedTransitioning) -> UIViewControllerInteractiveTransitioning? {
return self.interactive ? self : nil
}
ViewController上的按鈕觸發了一個模態segue來呈現NavigationController
NavigationController的委托出口鏈接到TransitionManager類的對象
NavigationController在屬性“navigationController”的TransitionManager類中引用
我認為關鍵問題是你在viewDidLoad
配置transitionDelegate
。 在這個過程中,這通常為時已晚。 您應該在init
導航控制器時執行此操作。
讓我們想象你的根場景(“Root”),它呈現導航控制器場景(“Nav”),然后從場景A推送到B到C,例如,我想象一個像這樣的對象模型,導航控制器只會擁有自己的動畫控制器,交互控制器和手勢識別器:
這是您在考慮(a)“root”呈現“nav”時的自定義轉換(非交互式)時所需要的; (b)當“nav”自行解散以返回“根”時的自定義轉換(交互與否)。 所以,我將導航控制器子類化為:
在其視圖中添加手勢識別器;
設置transitioningDelegate
以在從根場景轉換到導航控制器場景(並返回)時生成自定義動畫:
transitioningDelegate
還將返回交互控制器(僅在手勢識別器正在進行時才存在),如果您在手勢的上下文之外解除,則在手勢和非交互式轉換期間產生交互式轉換。
在Swift 3中,它看起來像:
import UIKit
import UIKit.UIGestureRecognizerSubclass
class CustomNavigationController: UINavigationController {
public required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
configure()
}
override init(rootViewController: UIViewController) {
super.init(rootViewController: rootViewController)
configure()
}
private func configure() {
transitioningDelegate = self // for presenting the original navigation controller
}
override func viewDidLoad() {
super.viewDidLoad()
delegate = self // for navigation controller custom transitions
let left = UIScreenEdgePanGestureRecognizer(target: self, action: #selector(handleSwipeFromLeft(_:)))
left.edges = .left
view.addGestureRecognizer(left)
}
fileprivate var interactionController: UIPercentDrivenInteractiveTransition?
func handleSwipeFromLeft(_ gesture: UIScreenEdgePanGestureRecognizer) {
let percent = gesture.translation(in: gesture.view!).x / gesture.view!.bounds.size.width
if gesture.state == .began {
interactionController = UIPercentDrivenInteractiveTransition()
if viewControllers.count > 1 {
popViewController(animated: true)
} else {
dismiss(animated: true)
}
} else if gesture.state == .changed {
interactionController?.update(percent)
} else if gesture.state == .ended {
if percent > 0.5 && gesture.state != .cancelled {
interactionController?.finish()
} else {
interactionController?.cancel()
}
interactionController = nil
}
}
}
// MARK: - UINavigationControllerDelegate
//
// Use this for custom transitions as you push/pop between the various child view controllers
// of the navigation controller. If you don't need a custom animation there, you can comment this
// out.
extension CustomNavigationController: UINavigationControllerDelegate {
func navigationController(_ navigationController: UINavigationController, animationControllerFor operation: UINavigationControllerOperation, from fromVC: UIViewController, to toVC: UIViewController) -> UIViewControllerAnimatedTransitioning? {
if operation == .push {
return ForwardAnimator()
} else if operation == .pop {
return BackAnimator()
}
return nil
}
func navigationController(_ navigationController: UINavigationController, interactionControllerFor animationController: UIViewControllerAnimatedTransitioning) -> UIViewControllerInteractiveTransitioning? {
return interactionController
}
}
// MARK: - UIViewControllerTransitioningDelegate
//
// This is needed for the animation when we initially present the navigation controller.
// If you're only looking for custom animations as you push/pop between the child view
// controllers of the navigation controller, this is not needed. This is only for the
// custom transition of the initial `present` and `dismiss` of the navigation controller
// itself.
extension CustomNavigationController: UIViewControllerTransitioningDelegate {
func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
return ForwardAnimator()
}
func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
return BackAnimator()
}
func interactionControllerForPresentation(using animator: UIViewControllerAnimatedTransitioning) -> UIViewControllerInteractiveTransitioning? {
return interactionController
}
func interactionControllerForDismissal(using animator: UIViewControllerAnimatedTransitioning) -> UIViewControllerInteractiveTransitioning? {
return interactionController
}
func presentationController(forPresented presented: UIViewController, presenting: UIViewController?, source: UIViewController) -> UIPresentationController? {
return PresentationController(presentedViewController: presented, presenting: presenting)
}
}
// When doing custom `present`/`dismiss` that overlays the entire
// screen, you generally want to remove the presenting view controller's
// view from the view hierarchy. This presentation controller
// subclass accomplishes that for us.
class PresentationController: UIPresentationController {
override var shouldRemovePresentersView: Bool { return true }
}
// You can do whatever you want in the animation; I'm just fading
class ForwardAnimator : NSObject, UIViewControllerAnimatedTransitioning {
func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
return 0.5
}
func animateTransition(using context: UIViewControllerContextTransitioning) {
let toView = context.viewController(forKey: .to)!.view!
context.containerView.addSubview(toView)
toView.alpha = 0.0
UIView.animate(withDuration: transitionDuration(using: context), animations: {
toView.alpha = 1.0
}, completion: { finished in
context.completeTransition(!context.transitionWasCancelled)
})
}
}
class BackAnimator : NSObject, UIViewControllerAnimatedTransitioning {
func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
return 0.5
}
func animateTransition(using context: UIViewControllerContextTransitioning) {
let toView = context.viewController(forKey: .to)!.view!
let fromView = context.viewController(forKey: .from)!.view!
context.containerView.insertSubview(toView, belowSubview: fromView)
UIView.animate(withDuration: transitionDuration(using: context), animations: {
fromView.alpha = 0.0
}, completion: { finished in
context.completeTransition(!context.transitionWasCancelled)
})
}
}
所以,我可以將故事板中導航控制器的基類更改為此自定義子類,現在根場景可以只顯示導航控制器(沒有特殊的prepare(for:)
),一切正常。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.