美文网首页
iOS 设备相关

iOS 设备相关

作者: 花前月下 | 来源:发表于2016-10-12 11:31 被阅读51次
    1.判断设备倾斜角度
    coreMotionMgr = CMMotionManager()
    coreMotionMgr!.deviceMotionUpdateInterval = 0.01
    let queue = NSOperationQueue()
    coreMotionMgr!.startDeviceMotionUpdatesUsingReferenceFrame(.XArbitraryZVertical, toQueue: queue) { (motion, nil) in 
        NSOperationQueue.mainQueue().addOperationWithBlock({ 
        let angle = atan2(motion!.gravity.y, motion!.gravity.x)
        let angleDegrees = angle * 180.0 / M_PI
           print("\(Int(angleDegrees)) 度")  // - 135 向左倾斜45度  -45 向右倾斜45度 向右放平是0度
         })
        }
    
    2.监听设备旋转
     override func viewDidLoad() {
           super.viewDidLoad()
           UIDevice.currentDevice().beginGeneratingDeviceOrientationNotifications()
           NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(handleDeviceOrientationDidChange), name: UIDeviceOrientationDidChangeNotification, object: nil)
     }
    
       // MARK: 监听设备旋转
       func handleDeviceOrientationDidChange() {
           let device = UIDevice.currentDevice()
           switch device.orientation {
           case .LandscapeLeft:
           case .LandscapeRight:
           case .Portrait:
           case .FaceDown:
           case .FaceUp: //...Unknown PortraitUpsideDown
           default: break
           }
       }
    
    3.强制横屏
      override func shouldAutorotate() -> Bool {
           return false
      }
        
      override func preferredInterfaceOrientationForPresentation() -> UIInterfaceOrientation {
         return UIInterfaceOrientation.LandscapeRight
      }
    

    相关文章

      网友评论

          本文标题:iOS 设备相关

          本文链接:https://www.haomeiwen.com/subject/hwmcettx.html