7.Swift 传感器

7.Swift 传感器

一定要真机上进行测试才能拿到数据

class ViewController: UIViewController,CLLocationManagerDelegate {

    private var cmm:CMMotionManager!
    private var queue:NSOperationQueue!
    //磁场传感器
    private var lm:CLLocationManager!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        self.cmm = CMMotionManager()
        self.queue = NSOperationQueue()
        self.lm = CLLocationManager()
        self.lm.delegate = self
    }

    func locationManager(manager: CLLocationManager, didUpdateHeading newHeading: CLHeading){
        NSLog("\(newHeading)")
    }

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

    // 启动加速度传感器
    private func startAccelerometerUpdatesToQueue(){
        self.cmm.accelerometerUpdateInterval = 1
        if(self.cmm.accelerometerAvailable && !self.cmm.accelerometerActive){
            self.cmm.startAccelerometerUpdatesToQueue(queue, withHandler: {
                (data:CMAccelerometerData?, err:NSError?) in
                NSLog("\(data)")
            })
        }else{
            NSLog("加速度传感器不可用")
        }
    }

    // 启动陀螺仪传感器
    private func startGyroUpdatesToQueue(){
        self.cmm.gyroUpdateInterval = 1;
        if(self.cmm.gyroAvailable && !self.cmm.gyroActive){
            self.cmm.startGyroUpdatesToQueue(queue, withHandler: {
                (data:CMGyroData?, err:NSError?) in
                NSLog("\(data)")
            })
        }else{
            NSLog("陀螺仪传感器不可用")
        }
    }

    // 启动距离传感器
    private func startListenerProximity(){
        UIDevice.currentDevice().proximityMonitoringEnabled = true
        NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("proximityChanged"), name: UIDeviceProximityStateDidChangeNotification, object: nil)
    }

    // 启动电源传感器
    private func startListenerBatteryLevel(){
        UIDevice.currentDevice().batteryMonitoringEnabled = true
        NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("batteryLevelChanged"), name: UIDeviceBatteryLevelDidChangeNotification, object: nil)
    }


    // 距离变化回调方法
    private func proximityChanged(){
        NSLog("\(UIDevice.currentDevice().proximityState)")
    }

    // 电量变化回调方法
    private func batteryLevelChanged(){
        NSLog("\(UIDevice.currentDevice().batteryLevel)")
    }


    // 停止电源传感器
    private func stopListenerBatteryLevel(){
        NSNotificationCenter.defaultCenter().removeObserver(self, name: UIDeviceBatteryLevelDidChangeNotification, object: nil)
    }

    // 停止距离传感器
    private func stopListenerProximity(){
        NSNotificationCenter.defaultCenter().removeObserver(self, name: UIDeviceProximityStateDidChangeNotification, object: nil)
    }

    // 停止加速度传感器
    private func stopAccelerometerUpdates(){
        if(self.cmm.accelerometerAvailable){
            self.cmm.stopAccelerometerUpdates()
        }
    }

    // 停止陀螺仪传感器
    private func stopGyroUpdates(){
        if(self.cmm.gyroAvailable){
            self.cmm.stopGyroUpdates()
        }
    }

    // ViewController将要呈现时
    override func viewWillAppear(animated: Bool) {
        self.startAccelerometerUpdatesToQueue()
        self.startGyroUpdatesToQueue()
        self.startListenerProximity()
        self.startListenerBatteryLevel()
        // 启动磁场传感器
        self.lm.startUpdatingHeading()
    }

    // ViewController将要消失时
    override func viewWillDisappear(animated: Bool) {
        self.stopAccelerometerUpdates()
        self.stopGyroUpdates()
        self.stopListenerProximity()
        self.stopListenerBatteryLevel()
    }

}

你可能感兴趣的:(传感器,数据,测试,Class)