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

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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: