iOS开发之传感器
判断当前设备离人或物的距离进行感应
//距离传感器
[UIDevice currentDevice].proximityMonitoringEnabled = YES;
直接上代码
//监听距离感应的通知
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(proximityChange:)
name:UIDeviceProximityStateDidChangeNotification
object:nil];
- (void)proximityChange:(NSNotificationCenter *)notification{
if ([UIDevice currentDevice].proximityState == YES) {
NSLog(@"某个物体靠近了设备屏幕"); // 屏幕会自动锁住
} else {
NSLog(@"某个物体远离了设备屏幕"); // 屏幕会自动解锁
}
}
手机方向
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(orientationChanged:)
name:@"UIDeviceOrientationDidChangeNotification"
object:nil];
- (void)orientationChanged:(NSNotification *)notification{
UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
NSLog(@"当前朝向枚举数字值 %ld",orientation);
switch (orientation) {
case UIDeviceOrientationPortrait:
NSLog(@"portrait ..."); //正立
break;
case UIDeviceOrientationPortraitUpsideDown:
NSLog(@"portrait UpsideDown ..."); //倒立
break;
case UIDeviceOrientationLandscapeLeft:
NSLog(@"Landscape Left ..."); //左横屏
break;
case UIDeviceOrientationLandscapeRight:
NSLog(@"Landscape Right ..."); //右横屏
break;
case UIDeviceOrientationFaceUp:
NSLog(@"Face Up ..."); //平放
break;
case UIDeviceOrientationFaceDown:
NSLog(@"Face Down ..."); //反放
break;
default:
break;
}
}