4

UIDeviceOrientationFaceDownを検出したい。これどうやってするの???実は、iPhoneの顔が平らな面に下がっているときに何かアクションを実行したいと思います。どうしてそれは可能ですか?plzは私がこれを行うのを助けます。

私はこのコードを持っていますが、このコードをどこにどのように適用するのかわかりません

 [[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 に答える