龙空技术网

iOS 监听、判断设备屏幕方向

前端全栈开发 18

前言:

此刻大家对“前端判断ios或者安卓”大致比较关注,小伙伴们都想要知道一些“前端判断ios或者安卓”的相关知识。那么小编也在网摘上收集了一些关于“前端判断ios或者安卓””的相关知识,希望朋友们能喜欢,各位老铁们一起来了解一下吧!

监听设备方向

deinit {    // 移除通知    NotificationCenter.default.removeObserver(self, name: UIDevice.orientationDidChangeNotification, object: nil)    // 结束    UIDevice.current.endGeneratingDeviceOrientationNotifications()} override func viewDidLoad() {    super.viewDidLoad()    // 注册    UIDevice.current.beginGeneratingDeviceOrientationNotifications()    // 监听 UIDevice.orientationDidChangeNotification    NotificationCenter.default.addObserver(self, selector: #selector(orientationDidChange), name: UIDevice.orientationDidChangeNotification, object: nil)} @objc fileprivate func orientationDidChange() {    switch UIDevice.current.orientation {    case .unknown:        print("unknown")    case .portrait:        print("竖屏")        setupGridView()    case .portraitUpsideDown:        print("颠倒竖屏")    case .landscapeLeft:        print("向左旋转横屏")        setupGridView()    case .landscapeRight:        print("向右旋转横屏")        setupGridView()    case .faceUp:        print("朝上")    case .faceDown:        print("朝下")    default:        break    }}
判断设备方向
if UIDevice.current.orientation.isPortrait {    print("竖屏")}if UIDevice.current.orientation.isLandscape {    print("横屏")}if UIDevice.current.orientation.isFlat {    print("水平")}if UIDevice.current.orientation.isValidInterfaceOrientation {    print("有效")}

标签: #前端判断ios或者安卓