iOS屏幕状态监听

iPad 适配

参考文章: http://www.jianshu.com/p/6ac34ab1ea24


手机旋转状态监听

方法一:

 
 
  1.        
  2. // 状态监听
  3. NotificationCenter.default.addObserver(self, selector: #selector(didOrientate(noti:)), name: NSNotification.Name.UIDeviceOrientationDidChange, object: nil)
  4. // 处理方法
  5.    func didOrientate(noti: NSNotification) {
  6.        print("宽 = ", self.view.frame.width)
  7.        let orient = UIDevice.current.orientation
  8.        switch orient {
  9.        case .portrait:
  10.            // 正
  11.            print("portrait")
  12.            break
  13.            
  14.        case .landscapeLeft:
  15.            print("landscapeLeft")
  16.            break
  17.            
  18.        case .landscapeRight:
  19.            print("landscapeRight")
  20.            break
  21.            
  22.        case .portraitUpsideDown:
  23.            print("portraitUpsideDown")
  24.            break
  25.            
  26.        default:
  27.            break
  28.        }
  29.    }


方法二:

 
 
  1. /**监听状态
  2.            UIApplicationDidChangeStatusBarOrientation
  3.            UIApplicationWillChangeStatusBarOrientation
  4. */
  5. NotificationCenter.default.addObserver(self, selector: #selector(statusBarOrientationChange(noti:)), name: NSNotification.Name.UIApplicationWillChangeStatusBarOrientation, object: nil)
  6. // 监听方法处理
  7. func statusBarOrientationChange(noti: NSNotification) {
  8.        let orienation = UIApplication.shared.statusBarOrientation
  9.        if orienation == .landscapeLeft {
  10.            print("左")
  11.        }
  12.        if orienation == .landscapeRight {
  13.            print("右")
  14.        }
  15.        if orienation == .portrait {
  16.            // 正
  17.            print("protrait")
  18.        }
  19.        if orienation == .portraitUpsideDown {
  20.            // 倒着
  21.            print("down")
  22.        }
  23.        if orienation == .unknown {
  24.            print("unknown")
  25.        }
  26.    }


方法三:

 
 
  1. // 重写父类方法,(貌似转场动画使用该方法)后续研究
  2. override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
  3.        print("宽1 = ", self.view.frame.width)
  4.        print("宽2 = ", size.width)
  5. }


方法四:

  
  
  1. // 重写父类监听方法,iOS8 已经弃用
  2. override func willRotate(to toInterfaceOrientation: UIInterfaceOrientation, duration: TimeInterval) {
  3.    print("will 宽 = ", self.view.frame.width)
  4. }
  5. override func didRotate(from fromInterfaceOrientation: UIInterfaceOrientation) {
  6.    print("did 宽 = ", self.view.frame.width)
  7.    print("*************************************")
  8. }


强制横屏:

  
  
  1. // AppDelegate中
  2. var changeOrientation: Bool = false
  3. /// 设置横屏/竖屏显示
  4. func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
  5.    if changeOrientation {
  6.        return [.landscapeLeft, .portrait, .landscapeRight]
  7.    }else {
  8.        return .portrait
  9.    }
  10. }
  11.    
  12. /// 被强制横屏的视图
  13. override func viewWillAppear(_ animated: Bool) {
  14.    super.viewWillAppear(animated)
  15.     (UIApplication.shared.delegate as! AppDelegate).changeOrientation = true
  16.        
  17.    //打开试图的横屏显示
  18.    let value = UIInterfaceOrientation.landscapeLeft.rawValue
  19.    UIDevice.current.setValue(value, forKey: "orientation")
  20. }
  21.    
  22. override func viewWillDisappear(_ animated: Bool) {
  23.    super.viewWillDisappear(animated)
  24.    (UIApplication.shared.delegate as! AppDelegate).changeOrientation = false
  25.    //将试图还原为竖屏
  26.    let value = UIInterfaceOrientation.portrait.rawValue
  27.    UIDevice.current.setValue(value, forKey: "orientation")
  28. }


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值