加速度 陀逻计的设备方向的使用

加速度计是一种能够感应设备一个方向上线性加速度的传感器 ios设备中有三种方向,为xyz轴, 为三轴的受力情况

先导入coremotion framework框架  在build phases 的link binary with libraries中 获取到coremotion framework 框架

随后导入 试这样的

加速度 陀逻计的设备方向的使用_第1张图片

随后在viewcontroller.swift中添加代码

import UIKit
//要获得加速度陀螺仪等 必须导入相应的类CoreMotion
import CoreMotion

class ViewController: UIViewController {
    //加速度获取
    var cmm:CMMotionManager?
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    //在viewdidload中加载
        cmm = CMMotionManager()
          }
    //在viewwillappear中获取 1表示每隔1秒钟获取 1/30表示1秒钟 获取30次
    override func viewWillAppear(animated: Bool) {
        cmm?.accelerometerUpdateInterval = 1
        //判断某些设备可用 比如模拟器就不可用
        if cmm!.accelerometerAvailable {
           //监听相关的数据
            //新添加一个线程 避免与主线程相碰撞 随后是一个闭包 表明获得数据后该呈现什么 有开始必有结束 则在viewdisappear中结束
            cmm!.startAccelerometerUpdatesToQueue(NSOperationQueue(), withHandler:
                { ( data:CMAccelerometerData!, err:NSError! ) -> Void in
                print(data)
            }
            )
            }
        else{
            var alert = UIAlertView(title:"曹凯强", message: "加速度计不能使用", delegate: self, cancelButtonTitle: "老曹")
            alert.show()
            
        }
    }
    override func viewWillDisappear(animated: Bool) {
        cmm?.stopAccelerometerUpdates()
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

陀螺计的使用 实质上也是和加速计差不多的 先获取 再判断能不能用 随后加入到队列中,在输出 最后判断是否还在活动 最后再关闭

import UIKit
import CoreMotion
class ViewController: UIViewController {
    //陀螺仪的获取
    var cmm:CMMotionManager?
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        //在viewdidload中加载
        cmm = CMMotionManager()
    }
    //在viewwillappear中获取 1表示每隔1秒钟获取 1/30表示1秒钟 获取30次
    override func viewWillAppear(animated: Bool) {
        cmm?.gyroUpdateInterval = 1
        //判断某些设备可用 比如模拟器就不可用
        if ((cmm?.gyroAvailable) != nil) {
            cmm?.startGyroUpdatesToQueue(NSOperationQueue(), withHandler: { (data: CMGyroData!, errr: NSError!) -> Void in
                print(data)
            })
        }
    }
    override func viewWillDisappear(animated: Bool) {
        if ((cmm?.gyroActive) != nil) {
            cmm?.stopGyroUpdates()
        }
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}



距离传感器的获取与使用

import UIKit
import CoreMotion
class ViewController: UIViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        
    }
    //在viewwillappear中获取 1表示每隔1秒钟获取 1/30表示1秒钟 获取30次
    override func viewWillAppear(animated: Bool) {
        //获得距离传感器
        UIDevice.currentDevice().proximityMonitoringEnabled = true
        //为该距离传感器添加事件监听器 在下面实现这函数
        NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("change"), name: UIDeviceOrientationDidChangeNotification, object: nil)
           }
//实现该函数
    func change() {
        print(">>>")
        //获得距离传感器相应的状态 就是有误障碍物
        print( (UIDevice.currentDevice().proximityState))
    }

    override func viewWillDisappear(animated: Bool) {
        //距离监听器消失
        NSNotificationCenter.defaultCenter().removeObserver(self)
           }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}
电源传感器的应用 主要是监听电池的电量如何

import UIKit
import CoreMotion
class ViewController: UIViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        
    }
       override func viewWillAppear(animated: Bool) {
        //获得电源传感器
        UIDevice.currentDevice().batteryMonitoringEnabled = true
        //为该电源传感器添加事件监听器 在下面实现这函数
        NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("change"), name: UIDeviceBatteryLevelDidChangeNotification, object: nil)
           }
//实现该函数
    func change() {
        print(">>>")
        //获得电池相应的状态
        print( (UIDevice.currentDevice().batteryLevel))
    }

    override func viewWillDisappear(animated: Bool) {
        //指定的电源监听器移除
        NSNotificationCenter.defaultCenter().removeObserver(self, name: UIDeviceBatteryLevelDidChangeNotification, object: nil)
           }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}
磁场传感器的实例 磁场传感器的获取必须得引入包 corelocation

import UIKit
import CoreLocation
class ViewController: UIViewController,CLLocationManagerDelegate {
    var lm :CLLocationManager!

       override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        //在viewdidload中加载
        lm = CLLocationManager()
    //实现代理
        lm.delegate = self
    }
        override func viewWillAppear(animated: Bool) {
    //获取磁场
    lm.startUpdatingHeading()
    }
    override func viewWillDisappear(animated: Bool) {
       
        }
    
//重写方法 实现代理时可以实现重写
    func locationManager(manager: CLLocationManager, didUpdateHeading newHeading: CLHeading) {
        print(newHeading)
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}




你可能感兴趣的:(加速度 陀逻计的设备方向的使用)