iOS 设备相关

2016-10-12  本文已影响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
  }
上一篇下一篇

猜你喜欢

热点阅读