首页 > 解决方案 > 没有插件的 MvvmCross iOS 汉堡菜单 iOS Native

问题描述

我想要什么

好的,这就是我想在 MvvmCross 中做的,没有任何插件,只是本机代码。我确实找到了有关如何使用它的教程,但我想在 MvvmCross.iOS 中找到它 看看我想在 MvvmCross.iOS 中做什么

请建议或转发更好的 MvvmCross.iOS 教程

要记住的要点

汉堡菜单应该像我链接的图像一样具有可拖动的效果

我试过的

ViewDidLoad() -->

UIPanGestureRecognizer gesture = new UIPanGestureRecognizer();


        gesture.AddTarget(() => HandleDrag(gesture));
        this.View.AddGestureRecognizer(gesture);

        panGestureRecognizer = new UIScreenEdgePanGestureRecognizer ( HandleSwipeRight);
        panGestureRecognizer.Edges = UIRectEdge.Left;
        this.View.AddGestureRecognizer(panGestureRecognizer);

HandleDrag() -->

        protected void HandleDrag(UIPanGestureRecognizer recognizer)
    {
        PointF offset2 = (System.Drawing.PointF)recognizer.TranslationInView(View);


        if (recognizer.State != (UIGestureRecognizerState.Cancelled | UIGestureRecognizerState.Failed
            | UIGestureRecognizerState.Possible))
        {
            Console.WriteLine("Here");
            // NEED TO LOAD ANOTHER VIEW HERE
            openMenu();

        }

    }

openMenu() -->

        public void openMenu()
    {
        viewBlack.Hidden = false;
        this.view.Hidden = false;
        UIView.Animate(
             duration: 0.3,
             delay: 0,
             options: UIViewAnimationOptions.CurveEaseInOut |
                 UIViewAnimationOptions.Autoreverse,
             animation: () =>
             {

            this.view.LayoutIfNeeded();
                 this.viewBlack.Alpha = this.maxBlackViewAlpha = 0.5f;
             },
             completion: () =>
             {
                 panGestureRecognizer.Enabled = false;
             }
        );
    }

隐藏菜单()-->

        public void closeMenu(){

        UIView.Animate(
    duration: 0.3,
    delay: 0,
    options: UIViewAnimationOptions.CurveEaseInOut |
        UIViewAnimationOptions.Autoreverse,
    animation: () =>
    {
            this.view.LayoutIfNeeded();
            this.viewBlack.Alpha = 0;
    },
    completion: () =>
    {
            panGestureRecognizer.Enabled = true;
            viewBlack.Hidden = true;
            view.Hidden = true;
    }
    );
    }

我的自定义汉堡菜单 UIView -->

            view = new UIView();
        view.Frame = new CGRect(0, 0, UIScreen.MainScreen.Bounds.Width / 1.1, UIScreen.MainScreen.Bounds.Height);
        var gradientLayer = new CAGradientLayer();
        gradientLayer.Colors = new[] { UIColor.FromRGB(64, 0, 128).CGColor, UIColor.FromRGB(0, 0, 128).CGColor };
        gradientLayer.Locations = new NSNumber[] { 0, 1 };
        gradientLayer.Frame = view.Frame;
        view.BackgroundColor = UIColor.Clear;
        view.Layer.AddSublayer(gradientLayer);
        var viewline = new UIView();
        viewline.Frame = new CGRect(20, 60, 100, 1);
        viewline.BackgroundColor = UIColor.White;
        var bb = new UIBarButtonItem();
        var Allbutton = new UIButton(new CGRect(0, 20, 135, 20));
        Allbutton.SetTitleColor(UIColor.Black, UIControlState.Normal);
        Allbutton.TitleLabel.BackgroundColor = UIColor.White;
        Allbutton.SetTitle("Login", UIControlState.Normal);
        var myPrefbutton = new UIButton(new CGRect(0, 120, 135, 20));
        myPrefbutton.SetTitleColor(UIColor.Black, UIControlState.Normal);
        myPrefbutton.SetTitle("Logout", UIControlState.Normal);
        myPrefbutton.TitleLabel.BackgroundColor = UIColor.White;
        view.BackgroundColor = UIColor.White;
        view.Add(Allbutton);
        view.Add(viewline);
        view.Add(myPrefbutton);
        view.Hidden = true;
        this.View.AddSubviews(view);

这是我能够从教程(SWIFT)转换为 MvvmCross.iOS 的唯一代码,它可以工作,但我无法拖动菜单来显示,发生的情况是它正常加载并且速度很快

笔记!!!我没有使用任何 Storyboards 或 nib 文件,只是使用这个汉堡菜单的纯代码

请仔细查看 .gif 注意菜单是可拖动的,这使得它的动画慢而不快。

如果我让你感到困惑,请不要难过我刚刚开始在 iOS 和 MvvmCross 中编码......我仍然是一个菜鸟

标签: c#iosxamarinxamarin.iosmvvmcross

解决方案


让它工作

首先必须创建一个 UIVew 类 -->

SideMenuView : MvxViewController

然后将 X 设置为减...如果用户选择 navbaritem,它将为零我还添加了一个覆盖 UIView

        viewBlack = new UIView();
        viewBlack.Frame = new CGRect(0, 0, UIScreen.MainScreen.Bounds.Width, UIScreen.MainScreen.Bounds.Height);
        viewBlack.BackgroundColor = UIColor.Black;
        viewBlack.Alpha = 0.5f;
        viewBlack.Hidden = true;
        this.View.AddSubviews(viewBlack);

推荐阅读