iPhone以编程方式设置界面方向 [英] iPhone set interfaceorientation programmatically

查看:82
本文介绍了iPhone以编程方式设置界面方向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试以编程方式更改单个UIViewController的方向.用户应在设置中选择视图是否为纵向.该值将保存在NSUserDefaults中,并在特定的ViewController中应更改方向.因此,如果Portrait在interfaceOrientation上,则应该在UIInterfaceorientationUpsideDown .......

I am trying to change the orientation of a single UIViewController programmatically. The user should choose in the settings if the view will be portrait or not. The value will be saved in NSUserDefaults and in the specific ViewController the orientation should be changed. So if Portrait is on the interfaceOrientation should be on UIInterfaceorientationUpsideDown......

我使用了以下方法,但是只有当用户将设备转到横向/纵向时,它才会改变方向...否则将不会触发任何操作.

I used following method but it will only change the orientation when the user will turn the device to landscape/portrait... otherwise nothing will be triggered.

- (BOOL)shouldAutoRotateToInterfactOrientation:(UIInterfaceOrientation)interfaceOrientation

此外,我尝试了下一个(也使用方法名称"setOrientation"):

Furthermore i tryied the next one (also with method name "setOrientation"):

[[UIDevice currentDevice] orientation: UIDeviceOrientationPortraitUpsideDown];

但是同样,什么也没有发生,只是出现错误,因为xcode表示没有使用该名称的方法.

But again, nothing happens, there just appear errors because xcode sayes there are no methods with this name.

但是,这应该是另一种以编程方式更改它的方式... 第一个方法也是在设备位置之后处理actionListener的方法,但是应该有一种直接调用此actionListener的方法.... 我只是查看了UIApplication文件的内部,但是没有有用的方法....

But ther should be another way to change it programatically... the first method is also just a method that handles the actionListener after device position but there should be a way to call this actionListener in a direct way.... I just looked inside the UIApplication File but there is no usefull method....

推荐答案

您应该使用类似以下代码的代码:

You should use something like this code:

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

然后,根据方向更改视图:

And then, change your view according to the orientation:

-(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

这篇关于iPhone以编程方式设置界面方向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆