12

メソッドに次のコードがあります。シミュレーターでこれを実行すると、デバッガーはコードをスキップしますか?私は何が欠けていますか?

if (([[UIDevice currentDevice] orientation] == UIDeviceOrientationLandscapeLeft) || 
        ([[UIDevice currentDevice] orientation] == UIDeviceOrientationLandscapeRight)) 
{       

} else {

}
4

8 に答える 8

54

インターフェイスの向きを決定する最善の方法は、ステータス バーの向きを確認することです。

 UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];

    if(orientation == UIInterfaceOrientationPortrait || 
       orientation == UIInterfaceOrientationPortraitUpsideDown) {

       //Portrait orientation

}

if(orientation == UIInterfaceOrientationLandscapeRight ||
   orientation == UIInterfaceOrientationLandscapeLeft) {

    //Landscape orientation

}

UIDeviceクラスは加速度計に基づいて方向を測定し、デバイスが平らに置かれている場合、正しい方向を返しません。

于 2011-06-14T13:23:06.413 に答える
23

マクロUIDeviceOrientationIsLandscapeとがあることに注意してくださいUIDeviceOrientationIsPortrait。したがって、LandscapeLeft および LandscapeRight と個別に比較する代わりに、次のようにすることができます。

if (UIDeviceOrientationIsLandscape([UIDevice currentDevice].orientation))
{
}
于 2009-12-01T06:49:55.437 に答える
18

アップデート2

これは問題ではありませんが、向きの通知をオンにしてみてください。

[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];


[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(detectOrientation) name:@"UIDeviceOrientationDidChangeNotification" object:nil];

アップデート

私の悪い、私はそれが空だと思った。

orステートメントを削除して、単一の方向をテストしてみてください。それで問題が解決するかどうかを確認してください。ブラケットの問題か何かばかげているかもしれません。

私は本番コードで次のテストを実行しているので、あなたのテクニックは機能するはずです:

    if (([[UIDevice currentDevice] orientation] == UIDeviceOrientationLandscapeLeft) || 
        ([[UIDevice currentDevice] orientation] == UIDeviceOrientationLandscapeRight)) {


}

元の回答

ステップインするには、実際にifブロックにステートメントを配置する必要があります。

デバッガーは、空のブロックをスキップするのに十分スマートです。

于 2009-05-30T18:56:29.380 に答える
2

オリエンテーション通知をオンにせずにこれを行う別の方法は、

ステップ 1:現在の向きをローカル変数に保存し、次のmyCurrentOrientationように割り当てます。

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
                                duration:(NSTimeInterval)duration
{
    myCurrentOrientation = toInterfaceOrientation;
}

ステップ 2:myCurrentOrientationチェックに使用する

if (UIInterfaceOrientationIsLandscape(myCurrentOrientation) == YES) {
    // landscape
}
else {
    // portrait.
}
于 2011-09-22T03:25:17.280 に答える
0

[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications] 値を取得する前に呼び出す必要があります。このメソッドのドキュメントをご覧ください。これを追跡するのにしばらく時間がかかりました。

于 2011-07-05T13:35:06.193 に答える
0

Springboard の微調整の中にいて、現在のアプリの向きに応じて何かを表示したい場合は、これを使用できます (ジェイルブレイクのみ):

UIInterfaceOrientation o = [[UIApplication sharedApplication] _frontMostAppOrientation];
于 2012-02-03T08:20:07.293 に答える
0

画面の向きと真の中心を見つける方法を次に示します。UIActivityIndi​​catorView を適切に設定できるように、Tuszy のメソッドを使用しました。

- (BOOL) isPortraitOrientation {
    UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
    if(orientation == UIInterfaceOrientationPortrait ||
       orientation == UIInterfaceOrientationPortraitUpsideDown) {
        return true;
    }
    if(orientation == UIInterfaceOrientationLandscapeRight ||
       orientation == UIInterfaceOrientationLandscapeLeft) {
        return false;
    }
    return false;
}

そしてセンターへの道は…

- (void) findMyUIViewCenter {
    CGPoint myCenter;
    if ([self isPortraitOrientation]) {
        myCenter = self.view.center;
    }
    else {
        myCenter = CGPointMake(self.view.frame.size.height / 2.0, self.view.frame.size.width / 2.0);
    }
    NSLog(@"true center -- x:%f y:%f )",myCenter.x,myCenter.y);
}
于 2014-05-22T10:39:00.263 に答える