【菜鸟初学Swift】IOS平台常用传感器的使用方式
来源:互联网 发布:淘宝开店零食起名字 编辑:程序博客网 时间:2024/06/04 19:20
一、加速度、陀螺仪、电源、距离传感器的使用方法:
import UIKitimport CoreMotion //传感器的使用,引入库 CoreMotionimport CoreLocation //磁场传感器的使用,引入库 CoreLocationclass 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 UIKitimport CoreLocation //磁场传感器的使用引入库:CoreLocationclass 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) { print(newHeading) } override func viewWillAppear(animated: Bool) { lm.startUpdatingHeading() }
不懂英文,学起来还是比较吃力的。又是英文基础良好的,学起来必定既轻松又好玩。
1 0
- 【菜鸟初学Swift】IOS平台常用传感器的使用方式
- iOS swift实现传感器
- ios 近距离传感器的使用
- swift 常用的跳转方式。
- iOS swift - swift常用的宏
- IOS的同步和异步,定位服务,常用功能使用,传感器
- iOS中加速度传感器的使用
- iOS中传感器的基本使用
- iOS传感器之气压计的使用
- ios开发 - SWift初学笔记
- iOS-传感器及使用
- 一、iOS初学笔记之swift的基础语法
- 夏普GP2Y1010AU0F灰尘传感器在STM32平台上的使用
- 传感器的使用:光线传感器
- 传感器的使用:加速度传感器
- 传感器的使用:方向传感器
- 关东升的iOS实战系列图书 《iOS实战:传感器卷(Swift版)》已经上市
- iOS Swift多线程、互斥锁的实现方式
- 1009.说反话
- 计算机科学的十大研究论文:The Top 10 research papers in computer science by Mendeley readership
- 获取当前选中的表单option的值
- 几种检验数字的方法区别 isNaN、isFinite
- iOS之微信支付交互图分析
- 【菜鸟初学Swift】IOS平台常用传感器的使用方式
- Activity生命周期
- Palindrome subsequence
- i++与++i的效率差别
- ASP.NET获取客户端、服务器端基础信息
- FZOJ 2188 过河I(bfs)
- 字符串相似算法-Jaro-Winkler Distance
- 【Android自定义View】制作老虎机Android版
- AHOI 基因匹配 dp LCS LIS