iOS- 传感器用法简介

<pre name="code" class="objc">//
//  ViewController.swift
//  Sensors
//
//  Created by 程磊 on 15/5/4.
//  Copyright (c) 2015年 程磊. All rights reserved.
//

import UIKit
import CoreMotion

class ViewController: UIViewController {
    
    var cmm: CMMotionManager!
    var q: NSOperationQueue!

    override func viewDidLoad() {
        super.viewDidLoad()
        cmm = CMMotionManager()
        q = NSOperationQueue()//创建一个线程
    }

    
    override func viewWillAppear(animated: Bool) {
        startAccelerometerUpdate()
        startGyroUpdate()
        startListenProximity()
        startListenLevel()
    }
    
    /**
    监听电量
    */
    func startListenLevel() {
        UIDevice.currentDevice().batteryMonitoringEnabled = true //打开电源监听
        NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("levelChanged"), name: UIDeviceBatteryLevelDidChangeNotification, object: nil)
    }
    
    func levelChanged() {
        println("level = \(UIDevice.currentDevice().batteryLevel)")//电量状态
    }
    
    /**
    移除监听
    */
    func stopListenLevel() {
        NSNotificationCenter.defaultCenter().removeObserver(self, name: UIDeviceBatteryLevelDidChangeNotification, object: nil)
    }
    
    /**
    开始获取距离传感器
    */
    func startListenProximity() {
        UIDevice.currentDevice().proximityMonitoringEnabled = true//是否监听距离传感器
        NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("proximityChanged"), name: UIDeviceProximityStateDidChangeNotification, object: nil)//加监听
    }
    
    /**
    距离状态改变,看其状态  true Or false
    */
    func proximityChanged() {
        println("proximity = \(UIDevice.currentDevice().proximityState)")
    }
    
    /**
    移除监听
    */
    func stopProximity() {
        NSNotificationCenter.defaultCenter().removeObserver(self)
    }
    
    /**
    开始获取加速的数据
    */
    func startAccelerometerUpdate() {
        cmm.accelerometerUpdateInterval = 1.0 //获取加速度的频率
        if cmm.accelerometerAvailable && !cmm.accelerometerActive {//判断加速度传感器是否可用,因为模拟器本身不带传感器
            cmm.startAccelerometerUpdatesToQueue(q, withHandler: { (data:CMAccelerometerData!, error:NSError!) -> Void in
                println("AccelerometerData = \(data)")
            })//获取加速的的值
        } else {
            println("加速度传感器不可用")
        }
    }
    /**
    开始获取陀螺仪数据
    */
    func startGyroUpdate() {
        cmm.gyroUpdateInterval = 1.0//获取频率
        if cmm.gyroAvailable && !cmm.gyroActive{//陀螺仪是否可用
            cmm.startGyroUpdatesToQueue(q, withHandler: { (data:CMGyroData!, error:NSError!) -> Void in
                println("GeroDta = \(data)")
            })//开始获取陀螺仪数据
        }
    }
    
    /**
    结束获取加速度数据
    */
    func stopAcceleratorUpdate() {
        if cmm.accelerometerActive {//判断传感器是否处在活跃状态,如果是则停掉
            cmm.stopAccelerometerUpdates()
        }
    }
    /**
    结束获取陀螺仪数据
    */
    func stopGyroUpdate() {
        if cmm.gyroActive {
            cmm.stopGyroUpdates()
        }
    }
    
    override func viewWillDisappear(animated: Bool) {
        stopAcceleratorUpdate()
        stopGyroUpdate()
        stopProximity()
        stopListenLevel()
    }
    
    


}



你可能感兴趣的:(iOS- 传感器用法简介)