加速计和陀螺仪

     //返回按钮事件
     @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. C# 一次查询多表,填充DataSet并指定表名

    lhrhi 原文 NET 一次查询多表,填充DataSet并指定表名(DataSet指定DataTable名称的技巧) 现实中的场景,有时可能需要一次查询数据库中表张.在使用SqlDataAdapte ...

  2. Devexpress DateEdit选年月 z

    Mask与Display只显示年月2012-02这种格式,但用户选择起来还是不爽,体验太差. 效果如下: 代码: using Microsoft.VisualBasic; using System; ...

  3. UITextView 不左上角显示

    在Autolayout中 UITextView显示不左上角显示,修改如下 在viewDidLoad里面添加如下代码 if([[[UIDevice currentDevice] systemVersio ...

  4. ubuntu 下 数学库编译链接时找不到各种数学问题解决方法 can not fon atan 等等

    解决参考 http://askubuntu.com/questions/190246/ld-cannot-find-math-library you should use -lm at the end ...

  5. 【九度OJ】题目1009-二叉搜索树

    题目 思路 构建二叉搜索树,并保存先序遍历和中序遍历的序列在samplePreOrder,sampleInOrder 每遇到一个新的序列,构建一棵二叉搜索树,保存先序遍历和中序遍历的序列testPre ...

  6. 多校5 1004 HDU5784 统计锐角三角形数目

    http://acm.hdu.edu.cn/showproblem.php?pid=5784 题意:n个点,找多少个锐角三角形数目 思路:极角排序+two pointers 当前选择的点集要倍增一倍, ...

  7. codeforce 702E Analysis of Pathes in Functional Graph RMQ+二进制

    http://codeforces.com/contest/702 题意:n个点,n条边,每个点出边只有一条,问从每个点出发经过k条边的边权和,以及边权最小值 思路: f[i][j] 第i个点出发,经 ...

  8. Ubuntu_wifi&pppoe

    学校现在上网全部要拨号,加上我在宿舍用的是无线路由,也就是要在ubuntu下实现连接wifi后再拨号,这个功能在默认的ubuntu网络设置里面是没有的,里面有dsl但是对有线网络使用的,有点小郁闷.不 ...

  9. linux shell 实现node-webkit的自动跨平台打包

    今天下午发现了个好玩的东西(node-webkit),这东西有一直是我想实现的功能:使用html编写桌面应用,实现跨平台: 具体实现方法:结合chrome浏览器内核和node.js搭建一个跨平台的应用 ...

  10. [POJ] #1001# Exponentiation : 大数乘法

    一. 题目 Exponentiation Time Limit: 500MS   Memory Limit: 10000K Total Submissions: 156373   Accepted: ...