简体   繁体   English

UIDevice.currentDevice()。beginGeneratingDeviceOrientationNotifications()无法正常工作

[英]UIDevice.currentDevice().beginGeneratingDeviceOrientationNotifications() not working

I am trying to use the property UIDevice.currentDevice().orientation, so I have something like the following 我正在尝试使用属性UIDevice.currentDevice()。orientation,所以我有类似以下内容

UIDevice.currentDevice().beginGeneratingDeviceOrientationNotifications()
var orientation = UIDevice.currentDevice().orientation
switch orientation {
  case .Portrait:
    print("portrait")
    break
  case .PortraitUpsideDown: // Device oriented vertically, home button on the top
    print("upsidedown")
    magneticHeading += 180
    magneticHeading = magneticHeading % 360
    break
  case .LandscapeLeft: // Device oriented horizontally, home button on the right
    print("left")
    magneticHeading -= 90
    magneticHeading = magneticHeading % 360
    break
  case .LandscapeRight: // Device oriented horizontally, home button on the left
    print("right")
    magneticHeading += 90
    magneticHeading = magneticHeading % 360
    break
  case .FaceUp: // Device oriented flat, face up
    print("up")
    break
  case .FaceDown: // Device oriented flat, face down
    print("down")
    break
  case .Unknown:
    print("unknown")
    break
  default:
    print("default")
  }

I've look through lots of webpages and questions on Stack Overflow, all of them uses beginGeneratingDeviceOrientationNotifications() then it will work. 我浏览了许多有关Stack Overflow的网页和问题,所有这些都使用beginGeneratingDeviceOrientationNotifications(),然后它将起作用。 However, my code won't work at all. 但是,我的代码根本无法工作。 In the switch statement, it always enter .Portrait no matter how I turn my phone. 在switch语句中,无论我如何转动手机,它始终输入.Portrait。 Does anyone know why? 有人知道为什么吗?

Also, I actually want the device's orientation, instead of my viewcontroller's orientation. 另外,我实际上想要的是设备的方向,而不是我的ViewController的方向。 I need to know how is the device held, so I need to use currentDevice().orientation 我需要知道设备的固定方式,所以我需要使用currentDevice()。

I've also tried using the following: 我也尝试使用以下方法:

var interfaceOrientation = UIApplication.sharedApplication().statusBarOrientation
switch interfaceOrientation {
  case .Unknown:
    print("1")
  case .Portrait:
    print("2")
  case .PortraitUpsideDown:
    print("3")
  case .LandscapeLeft:
    print("4")
  case .LandscapeRight:
    print("5")

again, no matter how I turn my phone, always enter .Portrait 同样,无论我如何转动手机,都必须输入.Portrait

在此处输入图片说明

Please check your xcode target general setting 请检查您的xcode目标常规设置

After a long time of trying, I figured that it's because I locked the screen on the phone. 经过长时间的尝试,我发现这是因为我锁定了手机的屏幕。 It seems to be no way to check if the user has locked the phone. 似乎无法检查用户是否已锁定手机。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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