これは、iPhoneのProximityStateを検出することで実現できます。[UIDevice currentDevice]
シングルトンを使用して、をに設定proximityMonitoringEnabled
しYES
ます。近接情報には、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(@"...");
}
}