簡體   English   中英

如何知道iDevice的當前方向

[英]How to know the current orientation of a iDevice

我正在為數學課程和練習做一個應用程序...

我只編寫了MainMenu,但是遇到了問題。 確實,我想知道目前我的工作方向。

但是,我嘗試了不同的方法來了解當前的方向,但是我沒有成功:

if UIDevice.current.orientation == UIDeviceOrientation.portrait {
            NSLog("Portrait")

        }

        else if UIDevice.current.orientation == UIDeviceOrientation.landscapeRight || UIDevice.current.orientation == UIDeviceOrientation.landscapeRight {
            NSLog ("Landscape")
        }

我也看到了這個答案,但是它不起作用(或者我不太了解): 在Swift中獲取設備方向 ,bu

我想在ViewDidLoad()中指定我在UIViewController中。

感謝您的幫助

我用按鈕設置了一個小的原型應用程序,該按鈕可在按下時檢查設備方向的變化。 這里是文檔的鏈接,希望對您的項目有所幫助。

class ViewController: UIViewController {

    let device = UIDevice.current

    override func viewDidLoad() {
        super.viewDidLoad()
        device.beginGeneratingDeviceOrientationNotifications()
    }

    @IBAction func orientation(_ sender: UIButton) {
        if device.isGeneratingDeviceOrientationNotifications {
            switch device.orientation {
            case .landscapeLeft:
                print("Landscape Left")
            case .portrait:
                print("Portrait")
            case .landscapeRight:
                print("Right")
            case .portraitUpsideDown:
                print("Upside down")
            case .unknown:
                print("Unknown")
            default:
                print("Else")
            }
        }
    }
}

暫無
暫無

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

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