码迷,mamicode.com
首页 > 移动开发 > 详细

ios 距离传感器和摇一摇

时间:2015-07-19 21:42:16      阅读:212      评论:0      收藏:0      [点我收藏+]

标签:

//距离传感器,以注册通知的形式来实现的

 

#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

ios 距离传感器和摇一摇

标签:

原文地址:http://www.cnblogs.com/tangranyang/p/4659525.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!