29.iPhone距离传感器的简单使用
2015-12-27 17:11
483 查看
上一章, 我们知道了陀螺仪传感器的使用方法, 现在让我们来看看下一个传感器, 距离传感器.
PS: 已经更新到Swift 2.1, 支持iOS 9.1
项目地址: 链接: http://pan.baidu.com/s/1i3g0A9Z 密码: qp8c
PS: 已经更新到Swift 2.1, 支持iOS 9.1
1.实现代码
[code]import UIKit import CoreMotion class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() } override func viewWillAppear(animated: Bool) { // 1.打开距离传感器 UIDevice.currentDevice().proximityMonitoringEnabled = true // 2.设置一个通知, 一旦距离传感器被使用, 就调用自定义的proximatyChanged方法 NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("proximatyChanged"), name: UIDeviceProximityStateDidChangeNotification, object: nil) } // 3.自定义proximatyChanged方法 func proximatyChanged() { print(">>>>>") print("\(UIDevice.currentDevice().proximityState)") } // 4.关掉页面之后, 把通知删除 override func viewDidDisappear(animated: Bool) { NSNotificationCenter.defaultCenter().removeObserver(self) } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() } }
2.最终效果
项目地址: 链接: http://pan.baidu.com/s/1i3g0A9Z 密码: qp8c
相关文章推荐
- 【机器学习】Softmax Regression简介
- lab 1实验报告
- 字符编码简介
- 编译C51项目的bat脚本
- Objc 精选 - 3D Touch 之 Peek & Pop 操作
- asp.net项目页面间传值的几种方式(部分转载)
- UVa 1433 - Aerodynamics
- CSharpGL(8)使用3D纹理渲染体数据 (Volume Rendering) 初探
- LeetCode Find Minimum in Rotated Sorted Array
- 3D Touch详解
- smbpasswd
- Leetcode: Additive Number
- “初心”-2012年终总结
- android软件引导界面
- 删除 右键--使用windows Defender扫描
- SpringMVC深度探险 —— SpringMVC核心配置文件详解
- spring-3.2.4.RELEASE整合quartz-2.2.2
- STL中vector iterators imcompatible报错
- CUDA笔记(十)
- 关于logfile sync