加速计和陀螺仪

     //返回按钮事件
     @IBAction func backButtonClick()
     {
         self.navigationController?.popViewControllerAnimated(true)
     }

     @IBOutlet var xLabel:UILabel!
     @IBOutlet var yLabel:UILabel!
     @IBOutlet var zLabel:UILabel!

     @IBOutlet var orientationLabel:UILabel!

     //加速计管理者-所有的操作都会由这个motionManager接管
     var motionManager:CMMotionManager!

     override func viewDidLoad() {
         super.viewDidLoad()

         titleLabel.text = titleString

         //------ CoreMotion 加速计
         motionManager = CMMotionManager()//注意CMMotionManager不是单例
         motionManager.accelerometerUpdateInterval = 0.1//设置读取时间间隔

         if motionManager.accelerometerAvailable//判断是否可以使用加速度计
         {
             //获取主线程并发队列,在主线程里跟新UI
             motionManager.startAccelerometerUpdatesToQueue(NSOperationQueue.mainQueue(), withHandler: { (var accelerometerData:CMAccelerometerData?, var error:NSError?) -> Void in

                 if error != nil
                 {
                     self.motionManager.stopAccelerometerUpdates()//停止使用加速度计
                 }else
                 {

                     self.xLabel.text = "x:\(accelerometerData!.acceleration.x)"
                     self.yLabel.text = "Y:\(accelerometerData!.acceleration.y)"
                     self.zLabel.text = "Z:\(accelerometerData!.acceleration.z)"
                 }
             })

         }else
         {
             let aler = UIAlertView(title: "您的设备不支持加速计", message: nil, delegate: nil, cancelButtonTitle: "OK")
             aler.show()
         }

         //感知设备方向-开启监听设备方向
         UIDevice.currentDevice().beginGeneratingDeviceOrientationNotifications()

         //添加通知,监听设备方向改变
         NSNotificationCenter.defaultCenter().addObserver(self, selector: "receivedRotation", name: UIDeviceOrientationDidChangeNotification, object: nil)

         //关闭监听设备方向
         UIDevice.currentDevice().endGeneratingDeviceOrientationNotifications()
     }

     override func didReceiveMemoryWarning() {
         super.didReceiveMemoryWarning()
         // Dispose of any resources that can be recreated.
     }

     // MARK: - 判断设备方向代理方法
     func receivedRotation()
     {
         var device = UIDevice.currentDevice()

         if device.orientation == UIDeviceOrientation.Unknown
         {
             orientationLabel.text = "Unknown"
         }
         else if device.orientation == UIDeviceOrientation.Portrait
         {
             orientationLabel.text = "Portrait"
         }
         else if device.orientation == UIDeviceOrientation.PortraitUpsideDown
         {
              orientationLabel.text = "PortraitUpsideDown"
         }
         else if device.orientation == UIDeviceOrientation.LandscapeLeft
         {
              orientationLabel.text = "LandscapeLeft"
         }
         else if device.orientation == UIDeviceOrientation.LandscapeRight
         {
              orientationLabel.text = "LandscapeRight"
         }else if device.orientation == UIDeviceOrientation.FaceUp
         {
              orientationLabel.text = "FaceUp"
         }
         else  if device.orientation == UIDeviceOrientation.FaceDown
         {
              orientationLabel.text = "FaceDown"
         }
     }

     // MARK: - 摇晃事件
     override func motionBegan(motion: UIEventSubtype, withEvent event: UIEvent) {

         println("motionBegan")//开始摇晃
     }

     override func motionEnded(motion: UIEventSubtype, withEvent event: UIEvent) {
         println("motionEnded")//摇晃结束
     }

     override func motionCancelled(motion: UIEventSubtype, withEvent event: UIEvent) {
         println("motionCancelled")//摇晃被意外终止
     }
 

ios开发——实用技术篇Swift篇&加速计和陀螺仪的更多相关文章

  1. ios开发——实用技术篇Swift篇&播放MP3

    播放MP3 // MARK: - 播放MP3 /*----- mp3 ------*/ //定时器- func updateTime() { //获取音频播放器播放的进度,单位秒 var cuTime ...

  2. ios开发——实用技术篇Swift篇&地址薄、短信、邮件

    //返回按钮事件 @IBAction func backButtonClick() { self.navigationController?.popViewControllerAnimated(tru ...

  3. ios开发——实用技术篇Swift篇&拍照

    拍照 // MARK: - 拍照 func fromPhotograph() { if UIImagePickerController.isSourceTypeAvailable(.Camera) { ...

  4. ios开发——实用技术篇Swift篇&照片选择

    照片选择 // MARK: - 选择照片 /*----- 选择照片 ------*/ @IBAction func addImageButtonClick() { let actionSheet = ...

  5. ios开发——实用技术篇Swift篇&系统声音

    系统声音 // MARK: - 系统声音 /*----- 系统声音 ------*/ @IBAction func systemSound() { //建立的SystemSoundID对象 var s ...

  6. ios开发——实用技术篇Swift篇&视频

    视频 // MARK: - 播放视频 /*----- 播放视频 ------*/ func moviePlayerPreloadFinish(notification:NSNotification) ...

  7. ios开发——实用技术篇Swift篇&录音

    录音 // MARK: - 录音 /*----- 录音 ------*/ var recorder:AVAudioRecorder? //录音器 var player:AVAudioPlayer? / ...

  8. ios开发——实用技术篇Swift篇&多点触摸与手势识别

    多点触摸与手势识别 //点击事件 var atap = UITapGestureRecognizer(target: self, action: "tapDo:") self.vi ...

  9. ios开发——实用技术篇OC篇&iOS的主要框架

    iOS的主要框架         阅读目录 Foundation框架为所有的应用程序提供基本系统服务 UIKit框架提供创建基于触摸用户界面的类 Core Data框架管着理应用程序数据模型 Core ...

随机推荐

  1. MySQL数据库分布式事务XA优缺点与改进方案

    1 MySQL 外部XA分析 1.1 作用分析 MySQL数据库外部XA可以用在分布式数据库代理层,实现对MySQL数据库的分布式事务支持,例如开源的代理工具:ameoba[4],网易的DDB,淘宝的 ...

  2. 动画 -- ListView列表item逐个出来(从无到有)

    import android.app.ListActivity; import android.os.Bundle; import android.widget.ArrayAdapter; publi ...

  3. 《Python 学习手册4th》 第十五章 文档

    ''' 时间: 9月5日 - 9月30日 要求: 1. 书本内容总结归纳,整理在博客园笔记上传 2. 完成所有课后习题 注:“#” 后加的是备注内容 (每天看42页内容,可以保证月底看完此书) “重点 ...

  4. XAMPP:访问phpmyadmin出错的解决方案

    来源:http://www.ido321.com/1246.html XAMPP(Apache+MySQL+PHP+PERL)是一个功能强大的建 XAMPP 软件站集成软件包,轻巧,用起来很方便.它提 ...

  5. leetcode@ [355] Design Twitter (Object Oriented Programming)

    https://leetcode.com/problems/design-twitter/ Design a simplified version of Twitter where users can ...

  6. 将大数据利用 BCP 导出SqlServer数据到CSV

    --导出数据 --BCP [数据库]..[数据库表] out "d:\abc.csv" -c -t "|" -T bcp "SELECT * FROM ...

  7. Phonegap学习点滴(2) -- 网络状态检测

    Phonegap学习点滴(2) -- 网络状态检测  http://blog.csdn.net/x251808026/article/details/16992943 方法一:在MainActivit ...

  8. Emacs和它的朋友们——阅读源代码篇(转)

    正如那本<Code Reading>一书中指出的那样,源代码阅读一直没有被很好的重 视:你上大学的时候有“代码阅读”这门课吗?相信没有. 1 Source Insight 谈到阅读源代码, ...

  9. ISO/IEC 14496 文档内容简介, MPEG标准

    ISO/IEC 14496是MPEG专家组制定的MPEG-4标准于1998年10月公布第1版,1999年1月成为国际标准,1999年12月公布了第2版,2000年初成为国际标准. 全文分为21个部分: ...

  10. Linux下文件的压缩与打包

    一.Linux下常见的文件压缩命令: 在Linux的环境中,压缩文件的扩展名大多是:『*.tar, *.tar.gz, *.tgz, *.gz, *.Z, *.bz2』,为什么会有这样的扩展名呢? 这 ...