断头台动画

2016-06-16  本文已影响41人  Jianhong_z

最近在做的一个小项目需要用到这种断头台效果,后面在github上找到了对应的代码。
https://github.com/Yalantis/GuillotineMenu
遗憾的是在菜单页面present一个ViewController的话,在这个ViewController dismiss后,菜单按钮会移到后面。
后面采用了一种取巧的方法,分别在下面的两处代码进行修改:

    private func animateDismissal(context: UIViewControllerContextTransitioning) {
        menu = context.viewControllerForKey(UITransitionContextFromViewControllerKey)!
        if menu.navigationController != nil {
            let toVC = context.viewControllerForKey(UITransitionContextToViewControllerKey)!
            context.containerView()!.addSubview(toVC.view)
            context.containerView()!.sendSubviewToBack(toVC.view)
        }
        if UIDevice.currentDevice().orientation == .LandscapeLeft || UIDevice.currentDevice().orientation == .LandscapeRight {
            updateChromeView()
            menu.view.addSubview(chromeView!)
        }
        
        let toVC = context.viewControllerForKey(UITransitionContextToViewControllerKey)
        toVC?.beginAppearanceTransition(true, animated: true)

        animationDelegate?.animatorWillStartDismissal?(self)

        //dimiss的时候转移到context.containerView()
        if containerMenuButton != nil {
            containerMenuButton!.removeFromSuperview();
            context.containerView()!.addSubview(containerMenuButton!)
        }

        animateMenu(menu.view, context: context)
    }
//MARK: - UIDynamicAnimatorDelegate protocol implementation
extension GuillotineTransitionAnimation: UIDynamicAnimatorDelegate {
 
    func dynamicAnimatorDidPause(animator: UIDynamicAnimator) {
        if self.mode == .Presentation {
            self.animator.removeAllBehaviors()
            menu.view.transform = CGAffineTransformIdentity
            menu.view.frame = animationContext.containerView()!.bounds
            anchorPoint = CGPointZero
        }

        chromeView?.removeFromSuperview()
        animationContext.completeTransition(true)
        
        if self.mode == .Presentation {
            let fromVC = animationContext.viewControllerForKey(UITransitionContextFromViewControllerKey)
            fromVC?.endAppearanceTransition()
            animationDelegate?.animatorDidFinishPresentation?(self)

            //present 结束的时候,把view转移到menu上
            if containerMenuButton != nil {
                containerMenuButton!.removeFromSuperview();
                menu.view.addSubview(containerMenuButton!);
            }

        } else {
            let toVC = animationContext.viewControllerForKey(UITransitionContextToViewControllerKey)
            toVC?.endAppearanceTransition()
            animationDelegate?.animatorDidFinishDismissal?(self)

        }
        //Stop displayLink
        displayLink.paused = true
    }
}

这样也可以在菜单页面present出 一个新的ViewController了。

上一篇 下一篇

猜你喜欢

热点阅读