码迷,mamicode.com
首页 > 移动开发 > 详细

ios开发——实用技术篇Swift篇&加速计和陀螺仪

时间:2015-06-07 23:08:29      阅读:211      评论:0      收藏:0      [点我收藏+]

标签:

加速计和陀螺仪

 

  1     //返回按钮事件
  2     @IBAction func backButtonClick()
  3     {
  4         self.navigationController?.popViewControllerAnimated(true)
  5     }
  6     
  7     
  8     @IBOutlet var xLabel:UILabel!
  9     @IBOutlet var yLabel:UILabel!
 10     @IBOutlet var zLabel:UILabel!
 11     
 12     @IBOutlet var orientationLabel:UILabel!
 13     
 14     //加速计管理者-所有的操作都会由这个motionManager接管
 15     var motionManager:CMMotionManager!
 16     
 17     override func viewDidLoad() {
 18         super.viewDidLoad()
 19         
 20         titleLabel.text = titleString
 21        
 22         
 23         
 24         //------ CoreMotion 加速计
 25         motionManager = CMMotionManager()//注意CMMotionManager不是单例
 26         motionManager.accelerometerUpdateInterval = 0.1//设置读取时间间隔
 27         
 28         if motionManager.accelerometerAvailable//判断是否可以使用加速度计
 29         {
 30             //获取主线程并发队列,在主线程里跟新UI
 31             motionManager.startAccelerometerUpdatesToQueue(NSOperationQueue.mainQueue(), withHandler: { (var accelerometerData:CMAccelerometerData?, var error:NSError?) -> Void in
 32                 
 33                 if error != nil
 34                 {
 35                     self.motionManager.stopAccelerometerUpdates()//停止使用加速度计
 36                 }else
 37                 {
 38                 
 39                     self.xLabel.text = "x:\(accelerometerData!.acceleration.x)"
 40                     self.yLabel.text = "Y:\(accelerometerData!.acceleration.y)"
 41                     self.zLabel.text = "Z:\(accelerometerData!.acceleration.z)"
 42                 }
 43             })
 44             
 45             
 46         }else
 47         {
 48             let aler = UIAlertView(title: "您的设备不支持加速计", message: nil, delegate: nil, cancelButtonTitle: "OK")
 49             aler.show()
 50         }
 51         
 52         
 53         
 54         //感知设备方向-开启监听设备方向
 55         UIDevice.currentDevice().beginGeneratingDeviceOrientationNotifications()
 56         
 57         //添加通知,监听设备方向改变
 58         NSNotificationCenter.defaultCenter().addObserver(self, selector: "receivedRotation", name: UIDeviceOrientationDidChangeNotification, object: nil)
 59         
 60         //关闭监听设备方向
 61         UIDevice.currentDevice().endGeneratingDeviceOrientationNotifications()
 62     }
 63 
 64     override func didReceiveMemoryWarning() {
 65         super.didReceiveMemoryWarning()
 66         // Dispose of any resources that can be recreated.
 67     }
 68     
 69     
 70     // MARK: - 判断设备方向代理方法
 71     func receivedRotation()
 72     {
 73         var device = UIDevice.currentDevice()
 74         
 75         if device.orientation == UIDeviceOrientation.Unknown
 76         {
 77             orientationLabel.text = "Unknown"
 78         }
 79         else if device.orientation == UIDeviceOrientation.Portrait
 80         {
 81             orientationLabel.text = "Portrait"
 82         }
 83         else if device.orientation == UIDeviceOrientation.PortraitUpsideDown
 84         {
 85              orientationLabel.text = "PortraitUpsideDown"
 86         }
 87         else if device.orientation == UIDeviceOrientation.LandscapeLeft
 88         {
 89              orientationLabel.text = "LandscapeLeft"
 90         }
 91         else if device.orientation == UIDeviceOrientation.LandscapeRight
 92         {
 93              orientationLabel.text = "LandscapeRight"
 94         }else if device.orientation == UIDeviceOrientation.FaceUp
 95         {
 96              orientationLabel.text = "FaceUp"
 97         }
 98         else  if device.orientation == UIDeviceOrientation.FaceDown
 99         {
100              orientationLabel.text = "FaceDown"
101         }
102     }
103     
104     // MARK: - 摇晃事件
105     override func motionBegan(motion: UIEventSubtype, withEvent event: UIEvent) {
106         
107         println("motionBegan")//开始摇晃
108     }
109     
110     override func motionEnded(motion: UIEventSubtype, withEvent event: UIEvent) {
111         println("motionEnded")//摇晃结束
112     }
113     
114     
115     override func motionCancelled(motion: UIEventSubtype, withEvent event: UIEvent) {
116         println("motionCancelled")//摇晃被意外终止
117     }

 

 

ios开发——实用技术篇Swift篇&加速计和陀螺仪

标签:

原文地址:http://www.cnblogs.com/iCocos/p/4559643.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!