2018年2月28日 星期三

orientation 螢幕方向

// 這個是裝置的實際方向
[UIDevice currentDevice].orientation

// 這個是螢幕的方向(如果你鎖住一些方向,他就不會顯示那些方向)
[UIApplication sharedApplication].statusBarOrientation


// 旋轉螢幕時的callback
 override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        NotificationCenter.default.addObserver(self, selector: #selector(deviceRotated), name: NSNotification.Name.UIDeviceOrientationDidChange, object: nil)
    }

    override func viewWillDisappear(_ animated: Bool) {
        NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UIDeviceOrientationDidChange, object: nil)
    }

    func deviceRotated(){
        if UIDeviceOrientationIsLandscape(UIDevice.current.orientation) {
            print("Landscape")
            // Resize other things
        }
        if UIDeviceOrientationIsPortrait(UIDevice.current.orientation) {
            print("Portrait")
            // Resize other things
        }
    }

沒有留言:

張貼留言