简体   繁体   中英

Determine Which Landscape mode to set Xamarin.iOS

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM