ios 距离传感器和摇一摇
2015-07-19 21:00
573 查看
//距离传感器,以注册通知的形式来实现的
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//1.开启距离传感器
[UIDevice currentDevice].proximityMonitoringEnabled=YES;
//2 注册通知进行监听
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(proximityMonitoring) name:UIDeviceProximityStateDidChangeNotification object:nil];
}
-(void)proximityMonitoring
{
BOOL state=[UIDevice currentDevice].proximityState;
if (state) {
NSLog(@"近");
}
else
{
NSLog(@"远");
}
}
-(void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
NSLog(@"手机自带,摇动手机触发事件");
}
-(void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
NSLog(@"摇动结束");
}
-(void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
NSLog(@"摇动被终止触发事件(来电了)");
}
-(void)dealloc
{
[[NSNotificationCenter defaultCenter]removeObserver:self];
}
@end
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//1.开启距离传感器
[UIDevice currentDevice].proximityMonitoringEnabled=YES;
//2 注册通知进行监听
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(proximityMonitoring) name:UIDeviceProximityStateDidChangeNotification object:nil];
}
-(void)proximityMonitoring
{
BOOL state=[UIDevice currentDevice].proximityState;
if (state) {
NSLog(@"近");
}
else
{
NSLog(@"远");
}
}
-(void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
NSLog(@"手机自带,摇动手机触发事件");
}
-(void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
NSLog(@"摇动结束");
}
-(void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
NSLog(@"摇动被终止触发事件(来电了)");
}
-(void)dealloc
{
[[NSNotificationCenter defaultCenter]removeObserver:self];
}
@end
相关文章推荐
- wxhl ios bj 49 张浩 学习心得连载 第一章
- iostream.h和iostream 区别,“cout”: 未声明的标识符”
- iOS开发中,关于什么时候使用点语法的解答
- iOS Touch ID使用
- iOS常用设计模式之观察者模式
- 一个仿IOS风格的AlertDialog
- iOS常用设计模式之委托模式
- iOS常用设计模式之单例模式
- IOS TableView滑动不灵敏问题
- IOS数组的排序和筛选
- IOS取消performSelector警告
- IOS艺术字及简单的图文混排
- IOS计算文字高度
- IOS之——快速获取苹果设备UDID(插曲)
- ios文章list
- ios文章list
- 28-手势 响应者链 手势识别器
- iOS设计模式-桥接
- iOS应用程序生命周期(前后台切换,应用的各种状态)详解 .
- ios 线程同步