簡體   English   中英

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

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

我正在嘗試使用屬性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")
  }

我瀏覽了許多有關Stack Overflow的網頁和問題,所有這些都使用beginGeneratingDeviceOrientationNotifications(),然后它將起作用。 但是,我的代碼根本無法工作。 在switch語句中,無論我如何轉動手機,它始終輸入.Portrait。 有人知道為什么嗎?

另外,我實際上想要的是設備的方向,而不是我的ViewController的方向。 我需要知道設備的固定方式,所以我需要使用currentDevice()。

我也嘗試使用以下方法:

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")

同樣,無論我如何轉動手機,都必須輸入.Portrait

在此處輸入圖片說明

請檢查您的xcode目標常規設置

經過長時間的嘗試,我發現這是因為我鎖定了手機的屏幕。 似乎無法檢查用戶是否已鎖定手機。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM