您的位置:首页 > 移动开发 > Swift

【菜鸟初学Swift】IOS平台常用传感器的使用方式

2015-12-08 12:39 507 查看

一、加速度、陀螺仪、电源、距离传感器的使用方法:

import UIKit
import CoreMotion   //传感器的使用,引入库 CoreMotion
import CoreLocation  //磁场传感器的使用,引入库 CoreLocation

class ViewController: UIViewController,CLLocationManagerDelegate {

var cmm:CMMotionManager! //创建类:CMMotionManager
var lm:CLLocationManager!

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

func StartAccerometer(){        //(一)、以下是获取加速度数据的使用方法

cmm.accelerometerUpdateInterval = 1    //获取频率,每1S获取一次
if cmm.accelerometerAvailable && !cmm.accelerometerActive{  //判断传感器是否为可用?且没活动

cmm.startAccelerometerUpdatesToQueue(NSOperationQueue(), withHandler: {(data:CMAccelerometerData?,err:NSError?) in
//传入加速度传感器数据

print("加速度数据:\(data)")    //打印加速度数据
})
}else{
print("加速度传感器不可用")
}
}

func StartGyrometer(){         //(二)、以下是获取陀螺仪数据的使用方法
cmm.gyroUpdateInterval = 1  //获取陀螺仪数据频率

if cmm.gyroAvailable && !cmm.gyroActive{   //判断传感器是否为可用?且没活动
cmm.startGyroUpdatesToQueue(NSOperationQueue(), withHandler: {
(data:CMGyroData?,err:NSError?) in
print("陀螺仪数据:\(data)")
})

}else{
print("陀螺仪传感器不可用")   }
}

func Startproximitymeter(){      //(三)、监听距离传感器状态的使用方法
UIDevice.currentDevice().proximityMonitoringEnabled = true  //启用距离传感器
NSNotificationCenter.defaultCenter().addObserver(self, selector:Selector("ProximityChanged"), name: UIDeviceProximityStateDidChangeNotification, object: nil)
}

func ProximityChanged(){
//  UIDevice.currentDevice().proximityState
//获取距离传感器状态,是否有障碍物,对应的结果:true 和 fault
print("\(UIDevice.currentDevice().proximityState)")
}

func StartListenBatteryLevel(){    //(四)、监听电量状态的方法
UIDevice.currentDevice().batteryMonitoringEnabled = true
NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("BatteryLevel"), name: UIDeviceBatteryLevelDidChangeNotification, object: nil)
}

func BatteryLevel(){
UIDevice.currentDevice().batteryState
print("\(UIDevice.currentDevice().batteryLevel)")
}

func locationManager(manager: CLLocationManager, didUpdateHeading newHeading: CLHeading) {
print(newHeading)

}

func StopBatteryLevel(){
NSNotificationCenter.defaultCenter().removeObserver(self, name: UIDeviceBatteryLevelDidChangeNotification, object: nil) //移除
}

func StopProximity(){
NSNotificationCenter.defaultCenter().removeObserver(self, name: UIDeviceProximityStateDidChangeNotification, object: nil) //移除

}

func StopAcceler(){
if cmm.accelerometerActive{                        //如果加速器传感器还在活动
cmm.stopAccelerometerUpdates()                     //停止侦听加速器传感器

}
}

func StopGyro(){
if cmm.gyroActive {
cmm.stopGyroUpdates()
}
}

override func viewWillAppear(animated: Bool) {     //view呈现出来的时候启动
StartAccerometer()
StartGyrometer()
Startproximitymeter()
StartListenBatteryLevel()
lm.startUpdatingHeading()

}

override func viewWillDisappear(animated: Bool) {      //程序界面消失
StopAcceler()
StopGyro()
StopProximity()
StopBatteryLevel()
}

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

}


二、磁场传感器的使用

import UIKit
import CoreLocation   //磁场传感器的使用引入库: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.
lm = CLLocationManager()
lm.delegate = self
}

func locationManager(manager: CLLocationManager, didUpdateHeading newHeading: CLHeading) {
p
rint(newHeading)
}

override func viewWillAppear(animated: Bool) {
lm.startUpdatingHeading()

}


不懂英文,学起来还是比较吃力的。又是英文基础良好的,学起来必定既轻松又好玩。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息