Esempio n. 1
0
        void SetupNavigationController(UISideMenuNavigationController forMenu, bool leftSide)
        {
            if (forMenu == null)
            {
                return;
            }

            var exitPanGesture = new UIPanGestureRecognizer();

            exitPanGesture.AddTarget(/*SideMenuTransition.self, */ () => SideMenuTransition.HandleHideMenuPan(exitPanGesture));
            forMenu.View.AddGestureRecognizer(exitPanGesture);
            forMenu.TransitioningDelegate  = SideMenuTransition.TransitioningDelegate;
            forMenu.ModalPresentationStyle = UIModalPresentationStyle.OverFullScreen;
            forMenu.LeftSide = leftSide;
            if (leftSide)
            {
                LeftSwipeToDismissGesture = exitPanGesture;
            }
            else
            {
                RightSwipeToDismissGesture = exitPanGesture;
            }
            UpdateMenuBlurIfNecessary();
        }
            // animate a change from one viewcontroller to another
            public override void AnimateTransition(IUIViewControllerContextTransitioning transitionContext)
            {
                // get reference to our fromView, toView and the container view that we should perform the transition in
                var container           = transitionContext.ContainerView;
                var menuBackgroundColor = _sideMenuTransition.SideMenuManager.AnimationBackgroundColor;

                if (menuBackgroundColor != null)
                {
                    container.BackgroundColor = menuBackgroundColor;
                }

                // create a tuple of our screens
                var screens = new
                {
                    from = transitionContext.GetViewControllerForKey(UITransitionContext.FromViewControllerKey),
                    to   = transitionContext.GetViewControllerForKey(UITransitionContext.ToViewControllerKey)
                };

                // assign references to our menu view controller and the 'bottom' view controller from the tuple
                // remember that our menuViewController will alternate between the from and to view controller depending if we're presenting or dismissing
                var menuViewController = (!_sideMenuTransition.Presenting ? screens.from : screens.to);
                var topViewController  = !_sideMenuTransition.Presenting ? screens.to : screens.from;

                var menuView = menuViewController.View;
                var topView  = topViewController.View;

                // prepare menu items to slide in
                if (_sideMenuTransition.Presenting)
                {
                    var tapView = new UIView();
                    tapView.AutoresizingMask = UIViewAutoresizing.FlexibleHeight | UIViewAutoresizing.FlexibleWidth;
                    var exitPanGesture = new UIPanGestureRecognizer();
                    exitPanGesture.AddTarget(/*SideMenuTransition.Current, */ () => _sideMenuTransition.HandleHideMenuPan(exitPanGesture));
                    var exitTapGesture = new UITapGestureRecognizer();
                    exitTapGesture.AddTarget(/*SideMenuTransition.Current, */ () => _sideMenuTransition.HandleHideMenuTap(exitTapGesture));
                    tapView.AddGestureRecognizer(exitPanGesture);
                    tapView.AddGestureRecognizer(exitTapGesture);
                    _sideMenuTransition.TapView = tapView;

                    _sideMenuTransition.originalSuperview = topView.Superview;

                    // add the both views to our view controller
                    switch (_sideMenuTransition.SideMenuManager.PresentMode)
                    {
                    case SideMenuManager.MenuPresentMode.ViewSlideOut:
                        container.AddSubview(menuView);
                        container.AddSubview(topView);
                        topView.AddSubview(tapView);
                        break;

                    case SideMenuManager.MenuPresentMode.MenuSlideIn:
                    case SideMenuManager.MenuPresentMode.MenuDissolveIn:
                    case SideMenuManager.MenuPresentMode.ViewSlideInOut:
                        container.AddSubview(topView);
                        container.AddSubview(tapView);
                        container.AddSubview(menuView);
                        break;
                    }

                    if (_sideMenuTransition.SideMenuManager.FadeStatusBar)
                    {
                        var blackBar = new UIView();
                        var menuShrinkBackgroundColor = _sideMenuTransition.SideMenuManager.AnimationBackgroundColor;
                        if (menuShrinkBackgroundColor != null)
                        {
                            blackBar.BackgroundColor = menuShrinkBackgroundColor;
                        }
                        else
                        {
                            blackBar.BackgroundColor = UIColor.Black;
                        }
                        blackBar.UserInteractionEnabled = false;
                        container.AddSubview(blackBar);
                        _sideMenuTransition.StatusBarView = blackBar;
                    }

                    _sideMenuTransition.HideMenuStart(); // offstage for interactive
                }

                // perform the animation!
                var duration = TransitionDuration(transitionContext);
                var options  = _sideMenuTransition.interactive ? UIViewAnimationOptions.CurveLinear : UIViewAnimationOptions.CurveEaseInOut;

                UIView.Animate(duration, 0, options,
                               animation: () =>
                {
                    if (_sideMenuTransition.Presenting)
                    {
                        _sideMenuTransition.PresentMenuStart();     // onstage items: slide in
                    }
                    else
                    {
                        _sideMenuTransition.HideMenuStart();
                    }
                    menuView.UserInteractionEnabled = false;
                },
                               completion: () =>
                {
                    // tell our transitionContext object that we've finished animating
                    if (transitionContext.TransitionWasCancelled)
                    {
                        var viewControllerForPresentedMenu = _sideMenuTransition.viewControllerForPresentedMenu;

                        if (_sideMenuTransition.Presenting)
                        {
                            _sideMenuTransition.HideMenuComplete();
                        }
                        else
                        {
                            _sideMenuTransition.presentMenuComplete();
                        }
                        menuView.UserInteractionEnabled = true;

                        transitionContext.CompleteTransition(false);


                        if (_sideMenuTransition.switchMenus)
                        {
                            _sideMenuTransition.switchMenus = false;
                            viewControllerForPresentedMenu?.PresentViewController(
                                _sideMenuTransition.PresentDirection == UIRectEdge.Left
                                        ? _sideMenuTransition.SideMenuManager.LeftNavigationController
                                        : _sideMenuTransition.SideMenuManager.RightNavigationController,
                                true, null);
                        }

                        return;
                    }

                    if (_sideMenuTransition.Presenting)
                    {
                        _sideMenuTransition.presentMenuComplete();
                        menuView.UserInteractionEnabled = true;
                        transitionContext.CompleteTransition(true);
                        switch (_sideMenuTransition.SideMenuManager.PresentMode)
                        {
                        case SideMenuManager.MenuPresentMode.ViewSlideOut:
                            container.AddSubview(topView);
                            break;

                        case SideMenuManager.MenuPresentMode.MenuSlideIn:
                        case SideMenuManager.MenuPresentMode.MenuDissolveIn:
                        case SideMenuManager.MenuPresentMode.ViewSlideInOut:
                            container.InsertSubview(topView, atIndex: 0);
                            break;
                        }

                        var statusBarView = _sideMenuTransition.StatusBarView;
                        if (statusBarView != null)
                        {
                            container.BringSubviewToFront(statusBarView);
                        }
                        return;
                    }

                    _sideMenuTransition.HideMenuComplete();
                    transitionContext.CompleteTransition(true);
                    menuView.RemoveFromSuperview();
                });
            }