距离传感器 和摇一摇功能

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //开启距离传感器监控开关
    [UIDevice currentDevice].proximityMonitoringEnabled = YES;

    //监控proximityStateDidChange,
    [[NSNotificationCenter defaultCenter]addObserver:self
                                            selector:@selector(proximityStateDidChange) name:UIDeviceProximityStateDidChangeNotification object:nil];
    

}

//判断距离传感器的状态 , 判断接近还是远离
-(void)proximityStateDidChange {
    
    if ([UIDevice currentDevice].proximityState) {
        
        NSLog(@"接近");
    }else{
        NSLog(@"远离");
    }
    
    
}

 

 

摇一摇功能:

-(void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event{
    
    NSLog(@"开始摇一摇");
    
}

-(void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event{
    
    NSLog(@"停止摇一摇");
    
    
}

 

posted @ 2016-04-25 11:30  o.0...w  阅读(186)  评论(0编辑  收藏  举报