【问题标题】:Xamarin iOS ObserveOrientationDidChange Landscape or PortraitXamarin iOS ObserveOrientationDidChange 横向或纵向
【发布时间】:2016-05-13 21:51:18
【问题描述】:

我需要检测用户何时将设备旋转到横向。我可以通过以下代码检测到方向何时发生变化:

UIDevice.CurrentDevice.BeginGeneratingDeviceOrientationNotifications();
this.orientationObserver = UIDevice.Notifications.ObserveOrientationDidChange(MyRotationCallback);

private void MyRotationCallback(object sender, NSNotificationEventArgs e)
{
    // Landscape or Portrait ?
}

它可以工作,但我不知道它是改为横向还是纵向。我怎样才能检测到这一点?

【问题讨论】:

    标签: c# ios xamarin xamarin.ios


    【解决方案1】:

    检查属性:

    UIDevice.CurrentDevice.Orientation
    

    价值观:

    LandscapeLeft
    PortraitUpsideDown
    LandscapeRight
    Portrait
    

    示例设置:

    在您的ViewDidLoad(或类似名称)中:

    Foundation.NSNotificationCenter.DefaultCenter.AddObserver(new NSString("UIDeviceOrientationDidChangeNotification"), DeviceRotated);
    

    委托/行动:

    void DeviceRotated(NSNotification notification)
    {
        System.Diagnostics.Debug.WriteLine(UIDevice.CurrentDevice.Orientation);
    }
    

    输出:

    LandscapeLeft
    PortraitUpsideDown
    LandscapeRight
    Portrait
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-12
      • 2011-07-01
      • 1970-01-01
      相关资源
      最近更新 更多