#import "ViewController.h" @interface Viewcontroller () @end @implementation viewcontroller-(void) viewdidload { [ Super Viewdidload]; Turn on the distance sensor monitor switch [uidevice currentdevice].proximitymonitoringenabled = YES; Monitoring Proximitystatedidchange, [[Nsnotificationcenter defaultcenter]addobserver:self selector: @selector ( Proximitystatedidchange) name:uideviceproximitystatedidchangenotification Object:nil]; } Judging the state of the distance sensor, judging whether it is near or far away-(void) Proximitystatedidchange { if ([Uidevice currentdevice].proximitystate) { NSLog (@ "Close"); } else{ NSLog (@ "Away"); } }
Shake a shake function:
-(void) Motionbegan: (uieventsubtype) Motion withevent: (uievent *) event{
NSLog (@ "Start shaking");
}
-(void) motionended: (uieventsubtype) Motion withevent: (uievent *) event{
NSLog (@ "Stop shaking");
}
Distance sensor and shake-shake function