Question

I want to detect UIDeviceOrientationFaceDown. How can i do this??? Actually i want to perform some action when my iphone face is down on flat surface. how is it possible??? plz help me to do this.

i have this code, but don't know where and how apply this code

 [[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];

if there is any tutorial then please suggest me that

Thanks in Advance and most welcome to your precious help and suggestions.

Was it helpful?

Solution

You can accomplish this by detecting the ProximityState of iPhone. Using the [UIDevice currentDevice] singleton, setting the proximityMonitoringEnabled to YES. you can access the proximity information through the proximityState property.

[[UIDevice currentDevice]proximityState];

iPhone has a sensor turns off the screen when you put it on your ear during a call, AFAIK that is an infrared sensor. and you can access it.

EDIT: You can also accomplish it using the below code. UIDeviceOrientationFaceDown The device is held parallel to the ground with the screen facing downwards. (regardless if it touched any object) if you want to know if the iPhone touched an object, detect the proximity state of device.

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

}

EDIT: to answer the question in comment. add this line in your viewDidLoad

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

then write a method

-(void)handleProximityChangeNotification{
     if([[UIDevice currentDevice]proximityState]){
        NSLog(@"...");
    }
}

OTHER TIPS

You can use the z value from accelerometer sensor (-1 <= z <= 1). If your device is facing down, the z value will be in 0 < z <=1 (0 when it's facing parallel to the ground, 1 when it's facing perpendicular to the ground). To get this data you can use 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
                              }

  }];

Don't forget to add CoreMotion framework to your project.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top