确定设置Xamarin.iOS的横向模式 [英] Determine Which Landscape mode to set Xamarin.iOS

查看:149
本文介绍了确定设置Xamarin.iOS的横向模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Xamarin.Forms中,我正在使用依赖项服务,通过使用下面的代码来强制屏幕在横向模式下定向.

UIDevice.CurrentDevice.SetValueForKey(new NSNumber((int)UIInterfaceOrientation.LandscapeLeft), new NSString("orientation"));

但是如何确定要在横向右"还是横向左"中定位

更新 当用户将设备握在横向右方并希望将方向锁定在横向时,设备将被锁定在横向左方.那么,如何智能地找到将其锁定在哪个位置呢?如果我将其锁定在横向"左侧,则当用户在横向右侧"中将其锁定时,它将无法正常工作.

找到了这个,可以这样做,但不确定如何做在Xamarin.iOS中

解决方案

在锁定方向之前,您可以根据iOS平台中的StatusBarOrientation检测设备方向.然后,您可以根据用户当前的方向锁定方向.

    var currentOrientation = UIApplication.SharedApplication.StatusBarOrientation;

    if (currentOrientation == UIInterfaceOrientation.LandscapeLeft)
    {
        //YOUR CODE
    }
    else if (currentOrientation == UIInterfaceOrientation.LandscapeRight)
    {
        //YOUR CODE
    }

参考:检查设备方向

更新:

锁定屏幕方向后,您需要一个旧的但有用的工具来检测方向.它是 Core Motion .例如,您可以像这样使用它:

        public void LockOrientation()
        {
            CMMotionManager CMManager = new CMMotionManager();
            CMManager.DeviceMotionUpdateInterval = 0.2f;
            CMManager.StartDeviceMotionUpdates(NSOperationQueue.MainQueue, (motion, error) => {
                if (Math.Abs(motion.Gravity.X) > Math.Abs(motion.Gravity.Y))
                {
                    Console.WriteLine("Lan");
                    if (motion.Gravity.X > 0)
                    {
                        UIDevice.CurrentDevice.SetValueForKey(new NSNumber((int)UIInterfaceOrientation.LandscapeLeft), new NSString("orientation"));
                        Console.WriteLine("Left");
                    }
                    else
                    {
                        UIDevice.CurrentDevice.SetValueForKey(new NSNumber((int)UIInterfaceOrientation.LandscapeRight), new NSString("orientation"));
                        Console.WriteLine("Right");
                    }
                }
                else
                {
                    if (motion.Gravity.Y >= 0)
                    {
                        Console.WriteLine("Down");
                    }
                    else
                    {
                        Console.WriteLine("UP");
                    }
                }

                CMManager.StopDeviceMotionUpdates();
            });
        }

In Xamarin.Forms I'm using dependency service, to force the screen to orient on Landscape mode by using the code below.

UIDevice.CurrentDevice.SetValueForKey(new NSNumber((int)UIInterfaceOrientation.LandscapeLeft), new NSString("orientation"));

But how to determine whether to orient in Landscape Right or Landscape Left

UPDATE When the user is holding the device in landscape right and wants to lock the orientation in the landscape, the device gets locked in Landscape Left. So how to intelligently find which position to lock it in? If I lock it in Landscape left it won't work when the user holds it in Landscape Right and vice versa.

Found this, equivalent of this would work, But not sure how to do it in Xamarin.iOS

解决方案

You could detect the device orientation according to StatusBarOrientation in the iOS platform before locking the orientation. Then you can lock the orientation according to user's current orientation.

    var currentOrientation = UIApplication.SharedApplication.StatusBarOrientation;

    if (currentOrientation == UIInterfaceOrientation.LandscapeLeft)
    {
        //YOUR CODE
    }
    else if (currentOrientation == UIInterfaceOrientation.LandscapeRight)
    {
        //YOUR CODE
    }

Reference: Checking Device Orientation

Update:

When the screen orientation is locked, you need an old but useful tool to detect the orientation. It is Core Motion. For example, you can use it like this:

        public void LockOrientation()
        {
            CMMotionManager CMManager = new CMMotionManager();
            CMManager.DeviceMotionUpdateInterval = 0.2f;
            CMManager.StartDeviceMotionUpdates(NSOperationQueue.MainQueue, (motion, error) => {
                if (Math.Abs(motion.Gravity.X) > Math.Abs(motion.Gravity.Y))
                {
                    Console.WriteLine("Lan");
                    if (motion.Gravity.X > 0)
                    {
                        UIDevice.CurrentDevice.SetValueForKey(new NSNumber((int)UIInterfaceOrientation.LandscapeLeft), new NSString("orientation"));
                        Console.WriteLine("Left");
                    }
                    else
                    {
                        UIDevice.CurrentDevice.SetValueForKey(new NSNumber((int)UIInterfaceOrientation.LandscapeRight), new NSString("orientation"));
                        Console.WriteLine("Right");
                    }
                }
                else
                {
                    if (motion.Gravity.Y >= 0)
                    {
                        Console.WriteLine("Down");
                    }
                    else
                    {
                        Console.WriteLine("UP");
                    }
                }

                CMManager.StopDeviceMotionUpdates();
            });
        }

这篇关于确定设置Xamarin.iOS的横向模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆