UINavigationController自定义转换问题

时间:2016-05-20 08:35:49

标签: ios uiview uiviewcontroller uinavigationcontroller uiviewanimationtransition

我遵循了许多有关如何创建自定义过渡的教程,但大多数教程适用于视图控制器的显示和解除。我只想将自定义转换应用于UINavigationController以进行推/弹动作。

阅读完苹果文档后,我创建了一个简单的项目来测试它是否有效。

以下是我自定义导航控制器的所有代码:

class CustomNC: UINavigationController {
    override func viewDidLoad() {
        super.viewDidLoad()
        delegate = self
    }
}

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

        print("Creating UIViewControllerAnimatedTransitioning for operation: \(operation.rawValue)")
        return self
    }
}

extension CustomNC: UIViewControllerAnimatedTransitioning {
    func transitionDuration(transitionContext: UIViewControllerContextTransitioning?) -> NSTimeInterval {
        print("Returning duration for transition")
        return 1.0
    }
    func animateTransition(transitionContext: UIViewControllerContextTransitioning) {
        print("Animating transition")
        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)

        // start the toView to the right of the screen
        toView.transform = offScreenRight

        // add the both views to our view controller
        container.addSubview(toView)
        container.addSubview(fromView)

        // get the duration of the animation
        // DON'T just type '0.5s' -- the reason why won't make sense until the next post
        // but for now it's important to just follow this approach
        let duration = self.transitionDuration(transitionContext)

        // perform the animation!
        // for this example, just slid both fromView and toView to the left at the same time
        // meaning fromView is pushed off the screen and toView slides into view
        // we also use the block animation usingSpringWithDamping for a little bounce
        UIView.animateWithDuration(
            duration,
            delay: 0.0,
            usingSpringWithDamping: 0.5,
            initialSpringVelocity: 0.8,
            options: [],
            animations: {
                fromView.transform = offScreenLeft
                toView.transform = CGAffineTransformIdentity
            }, completion: { finished in
                // tell our transitionContext object that we've finished animating
                transitionContext.completeTransition(true)
        })
    }
}

然后我只创建两个默认视图控制器First和Second。首先让UIButton对第二个展示行动。首先嵌入在NavigationController中,此NavigationController将Custom Class设置为CustomNC。

My Storyboard

运行后我看到V1控制器,但是当我点击UIButton应用程序崩溃时没有任何错误消息。在日志中,我只看到那些行:

Creating UIViewControllerAnimatedTransitioning for operation: 1
Returning duration for transition
(lldb)

据我所知,委托是可见的,导航控制器正在使用此委托。转换开始时它会获得动画持续时间间隔,但永远不会调用animateTransition方法,因为我在日志中看不到“动画转换”消息。

有人能指出我有问题吗?

0 个答案:

没有答案