2

プログラムで単一の向きを変更しようとしていUIViewControllerます。ビューを縦向きにするかどうかは、ユーザーが設定で選択する必要があります。値が保存さNSUserDefaultsれ、特定ViewControllerの向きで変更する必要があります。ポートレートがオンの場合はオンにするinterfaceOrientation必要がありますUIInterfaceorientationUpsideDown……

私は次の方法を使用しましたが、ユーザーがデバイスを横向き/縦向きに変えた場合にのみ向きが変わります...それ以外の場合は何もトリガーされません。

- (BOOL)shouldAutoRotateToInterfactOrientation:(UIInterfaceOrientation)interfaceOrientation

さらに、次のものを試しました(メソッド名「setOrientation」も使用):

[[UIDevice currentDevice] orientation: UIDeviceOrientationPortraitUpsideDown];

xcodeはこの名前のメソッドがないと言っているため、エラーが表示されます。

しかし、プログラムでそれを変更する別の方法があるはずです...最初のメソッドは、デバイスの位置の後にactionListenerを処理する単なるメソッドですが、これactionListenerを直接呼び出す方法があるはずです....UIApplicationファイルですが、便利な方法はありません....

4

4 に答える 4

6

次のコードのようなものを使用する必要があります。

[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(rotationChanged:)
                                             name:@"UIDeviceOrientationDidChangeNotification"
                                           object:nil];

次に、方向に応じてビューを変更します。

-(void)rotationChanged:(NSNotification *)notification{
    NSInteger orientation = [[UIDevice currentDevice] orientation];
    UIWindow *_window = [[[UIApplication sharedApplication] delegate] window];
    switch (orientation) {
        case 1:
            [UIView beginAnimations:nil context:nil];
            [UIView setAnimationDuration:0.4];

            [_window setTransform:CGAffineTransformMakeRotation (0)];
            [_window setFrame:CGRectMake(0, 0, 320, 480)];
            [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait animated:YES];

            [UIView commitAnimations];
            break;
        case 2:
            [UIView beginAnimations:nil context:nil];
            [UIView setAnimationDuration:0.4];

            [_window setTransform:CGAffineTransformMakeRotation (M_PI)];
            [_window setFrame:CGRectMake(0, 0, 320, 480)];
            [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait animated:YES];

            [UIView commitAnimations];
            break;
        case 3:
            [UIView beginAnimations:nil context:nil];
            [UIView setAnimationDuration:0.4];

            [_window setTransform:CGAffineTransformMakeRotation (M_PI / 2)];
            [_window setFrame:CGRectMake(0, 0, 320, 480)];
            [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight animated:YES];

            [UIView commitAnimations];
            break;
        case 4:
            [UIView beginAnimations:nil context:nil];
            [UIView setAnimationDuration:0.4];

            [_window setTransform:CGAffineTransformMakeRotation (- M_PI / 2)];
            [_window setFrame:CGRectMake(0, 0, 320, 480)];
            [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeLeft animated:YES];

            [UIView commitAnimations];
            break;
        default:
            break;
    }
}

それが役立つことを願っています:P

于 2012-06-20T00:10:27.577 に答える
2

iOS7これを使用すると、以前に完全に機能します。

[[UIDevice currentDevice] setValue:
           [NSNumber numberWithInteger: UIInterfaceOrientationPortrait]
                            forKey:@"orientation"];
于 2014-08-27T12:32:38.413 に答える
0
[[UIApplication sharedApplication] setStatusBarOrientation:yourDesiredOrientation];

次に、shouldAutoRotateメソッドを編集する必要があります(次の例はポートレート用です:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
 return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
于 2012-06-19T21:27:36.060 に答える