动画

最近在做的一个小项目需要用到这种断头台效果,后面在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了。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 去参加朋友的婚宴,觥筹交错之后,桌上还剩下很多菜,有的甚至就动过一两筷子。妻子招呼服务员拿饭盒过来打包,我说...
    石在山中阅读 174评论 0 0
  • 学 知识和技能;知识(五经:诗 书 礼 乐 易)技能(六艺:礼 乐 射 御 书 数)通过这种学习成为有用的人才。 ...
    张MAX阅读 595评论 0 0
  • 读《富爸爸穷爸爸》 这是我第二次读这本书,再次读这本书我又有了新的感悟: 1:做金钱的主人。钱是一种很重要的东西,...
    永永_99阅读 212评论 0 0