Determine Which Landscape mode to set Xamarin.iOS

我是研究僧i 提交于 2019-12-11 14:17:13

问题


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


回答1:


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();
            });
        }


来源:https://stackoverflow.com/questions/46912358/determine-which-landscape-mode-to-set-xamarin-ios

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!