Rox*_*Rox 4 iphone objective-c ios5 uideviceorientation
我想检测UIDeviceOrientationFaceDown.我怎样才能做到这一点???实际上我想在我的iphone面朝下平面时执行一些操作.这怎么可能???请帮助我这样做.
我有这个代码,但不知道在何处以及如何应用此代码
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
BOOL getOrientationUpdates = [[UIDevice currentDevice] isGeneratingDeviceOrientationNotifications];
NSLog(@"will receive orientation notifications: %@", getOrientationUpdates?@"YES":@"NO");
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(orientationChanged:)
name:UIDeviceOrientationDidChangeNotification
object:nil];
Run Code Online (Sandbox Code Playgroud)
如果有任何教程,请建议我
在此先感谢,非常欢迎您的宝贵帮助和建议.
您可以通过检测iPhone的ProximityState来实现此目的.使用[UIDevice currentDevice]单例,设置proximityMonitoringEnabled为YES.您可以通过proximityState属性访问邻近信息.
[[UIDevice currentDevice]proximityState];
Run Code Online (Sandbox Code Playgroud)
iPhone有一个传感器在通话过程中关闭屏幕,AFAIK是一个红外传感器.你可以访问它.
编辑:您也可以使用以下代码完成它.UIDeviceOrientationFaceDown
The device is held parallel to the ground with the screen facing downwards.(无论是否触摸任何物体)如果您想知道iPhone是否触摸了物体,请检测设备的接近状态.
[[NSNotificationCenter defaultCenter] removeObserver:self];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(detectOrientation) name:@"UIDeviceOrientationDidChangeNotification" object:nil];
-(void)detectOrientation;{
switch ([[UIDevice currentDevice] orientation]) {
case UIDeviceOrientationPortrait:
{
NSLog(@"portrait");
}
break;
case UIDeviceOrientationPortraitUpsideDown:
{
NSLog(@"portraitUpSideDown");
}
break;
case UIDeviceOrientationLandscapeLeft:
{
NSLog(@"landscapeLeft");
}
break;
case UIDeviceOrientationLandscapeRight:
{
NSLog(@"landscapeRight");
}
break;
case UIDeviceOrientationFaceDown:
{
NSLog(@"facedown!!");
}
break;
default:
break;
}
}
}
Run Code Online (Sandbox Code Playgroud)
编辑:回答评论中的问题.在viewDidLoad中添加此行
[UIDevice currentDevice].proximityMonitoringEnabled = YES;
[[NSNotificationCenter defaultCenter] removeObserver:self];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleProximityChangeNotification:) name:UIDeviceProximityStateDidChangeNotification object:nil];
Run Code Online (Sandbox Code Playgroud)
然后写一个方法
-(void)handleProximityChangeNotification{
if([[UIDevice currentDevice]proximityState]){
NSLog(@"...");
}
}
Run Code Online (Sandbox Code Playgroud)