没有插件的 MvvmCross iOS 汉堡菜单 iOS Native

Posted

技术标签:

【中文标题】没有插件的 MvvmCross iOS 汉堡菜单 iOS Native【英文标题】:MvvmCross iOS hamburger Menu without plugins iOS Native 【发布时间】:2018-10-12 11:09:06 【问题描述】:

好的,这就是我想在 MvvmCross 中做的,没有任何插件,只是本地代码。我确实找到了一个教程,但我想在 MvvmCross.ios Have a look at what I would like to do in 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;
             
        );
    

hideMenu() -->

        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 的唯一代码,它可以工作,但是 我无法拖动菜单显示,发生的情况是它正常加载并且速度很快

注意!!!我没有使用任何 Storyboard 或 nib 文件,只是为这个汉堡菜单使用纯代码

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

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

【问题讨论】:

【参考方案1】:

开始工作

首先要创建一个 UIVew 类 -->

SideMenuView : MvxViewController

然后将 X 设置为减号...如果用户选择导航栏项,它将为零我还添加了一个覆盖 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);

【讨论】:

以上是关于没有插件的 MvvmCross iOS 汉堡菜单 iOS Native的主要内容,如果未能解决你的问题,请参考以下文章

张高兴的 Xamarin.Forms 开发笔记:为 Android 与 iOS 引入 UWP 风格的汉堡菜单 ( MasterDetailPage )

MvvmCross picturechoosen 插件和相机卡住问题

Xamarin.Forms Android 保留汉堡包/菜单图标而不是后退按钮

为啥我的 React + Tailwind 汉堡菜单没有关闭?

如何将汉堡菜单更改为常规菜单?

如何在 iOS 中使用没有故事板的 MvvmCross?