簡體   English   中英

即使方向被鎖定,我如何檢測設備方向

[英]How can i detect device orientation even if the orientation is locked

我使用了磁力計,但它的值總是不同的。我想檢測用戶設備何時在橫向位置處於水平平均值,並且在特定發布時想要執行某些操作。

在 AppDelegate.swift 中的 didFinishLaunchingWithOptions 函數中放置以下代碼:

NotificationCenter.default.addObserver(self, selector: #selector(AppDelegate.rotated), name: UIDevice.orientationDidChangeNotification, object: nil)

然后在 AppDelegate 中放入以下函數:

func rotated() {
    if UIDeviceOrientationIsLandscape(UIDevice.current.orientation) {
        print("Landscape")
    }

    if UIDeviceOrientationIsPortrait(UIDevice.current.orientation) {
        print("Portrait")
    }
}

希望它對你有用... :)

暫無
暫無

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

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