Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIDeviceOrientationFaceDown in iphone

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.

like image 457
Rox Avatar asked Dec 27 '22 19:12

Rox


1 Answers

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(@"...");
    }
}
like image 83
janusfidel Avatar answered Jan 13 '23 21:01

janusfidel