关于swift:UINavigationControllerDelegate方法,在iOS 8中使用导航控制器进行自定义转换

UINavigationControllerDelegate methods, custom transitioning using navigation controller in ios 8

我尝试在弹出或推送导航控制器时创建自定义过渡。 我创建了一个符合UINavigationControllerDelegate的transitionManager类。 我已经创建了这个transitionManager的对象并将其作为transitioningDelegate添加到我的navigationController中。

推送动画运行的很好,但是当我尝试跳回到上一个viewController时,我只会看到黑屏。

我已经遍历了许多其他文章来使其运行并尝试了手头的一切,但是当我弹出时它仍然没有显示以前的ViewController。

这里的transitionManager的代码是:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import UIKit

class BRTransitionManager: NSObject, UINavigationControllerDelegate, UIViewControllerAnimatedTransitioning {

    private var presenting = true

    func navigationController(navigationController: UINavigationController, animationControllerForOperation operation: UINavigationControllerOperation, fromViewController fromVC: UIViewController, toViewController toVC: UIViewController) -> UIViewControllerAnimatedTransitioning? {

        if  operation == UINavigationControllerOperation.Push{
            self.presenting = true
        } else if operation == UINavigationControllerOperation.Pop{
            self.presenting = false
        }

        return self
    }

    func animateTransition(transitionContext: UIViewControllerContextTransitioning) {
        let container = transitionContext.containerView()
        let fromView = transitionContext.viewForKey(UITransitionContextFromViewKey)!
        let toView = transitionContext.viewForKey(UITransitionContextToViewKey)!

        // set up from 2D transforms that we'll use in the animation
        let offScreenRight = CGAffineTransformMakeTranslation(container.frame.width, 0)
        let offScreenLeft = CGAffineTransformMakeTranslation(-container.frame.width, 0)

        // prepare the toView for the animation
        if (self.presenting == true) {
            // add the both views to our view controller
            container.addSubview(toView)
            // container.addSubview(fromView)

            toView.transform = offScreenRight
        } else {
            toView.transform = offScreenLeft
        }

        let duration = self.transitionDuration(transitionContext)
        UIView.animateWithDuration(duration, delay: 0.0, usingSpringWithDamping: 0.5, initialSpringVelocity: 0.8, options: nil, animations: {

            if (self.presenting == true) {
                fromView.transform = offScreenLeft
            } else {
                fromView.transform = offScreenRight
            }

            toView.transform = CGAffineTransformIdentity

        }, completion: { finished in

            transitionContext.completeTransition(true)
        })
    }

    func transitionDuration(transitionContext: UIViewControllerContextTransitioning) -> NSTimeInterval {
        return 0.8
    }  
}


UINavigationController维护对其委托的弱引用。如果没有强引用,则转换完成后,将释放UINavigationControllerDelegate对象。

您可以在情节提要编辑器中设置委托对象。通过将对象从面板中拖到"第一响应者"和"退出"图标之间的"导航控制器"场景中来添加对象。将其类设置为您的委托对象的类。确保该模块是您当前的应用程序模块。然后,从导航控制器图标拖动到对象图标,然后从弹出菜单中选择"委托"。

这似乎对委托对象保持了强烈的引用。然后,委托对象将可用于调用展开过渡。您不需要自己创建对象或设置委托引用。

我在Scott James Remnant的博客文章中发现了这种技术。

Scott James Remnant-自定义iOS Segues,过渡和动画-正确的方法