4

我想检测 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];

如果有任何教程那么请建议我

在此先感谢您,非常欢迎您提供宝贵的帮助和建议。

4

2 回答 2

8

您可以通过检测 iPhone 的 ProximityState 来完成此操作。使用[UIDevice currentDevice]单例,设置proximityMonitoringEnabledYES. 您可以通过proximityState 属性访问接近度信息。

[[UIDevice currentDevice]proximityState];

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;
        }
    } 

}

编辑:回答评论中的问题。在您的 viewDidLoad 中添加这一行

[UIDevice currentDevice].proximityMonitoringEnabled = YES;
[[NSNotificationCenter defaultCenter] removeObserver:self];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleProximityChangeNotification:) name:UIDeviceProximityStateDidChangeNotification object:nil];

然后写一个方法

-(void)handleProximityChangeNotification{
     if([[UIDevice currentDevice]proximityState]){
        NSLog(@"...");
    }
}
于 2012-08-23T11:27:39.767 回答
2

您可以使用加速度计传感器的 z 值 (-1 <= z <= 1)。如果您的设备朝下,z 值将在 0 < z <=1(当它与地面平行时为 0,当它与地面垂直时为 1)。要获取此数据,您可以使用CMMotionManager

 #import <CoreMotion/CoreMotion.h> 

 CMMotionManager *cm=[[CMMotionManager alloc] init];
 cm.deviceMotionUpdateInterval=0.2f;
 [cm startDeviceMotionUpdatesToQueue:[NSOperationQueue mainQueue]
                            withHandler:^(CMDeviceMotion *data, NSError *error) {
                              if(data.gravity.z>=0.3f)// 

                              {
                                 //DO something
                              }

  }];

不要忘记将CoreMotion框架添加到您的项目中。

于 2015-08-22T02:08:48.840 回答