如何在设备旋转时旋转和调整UIView的大小

时间:2013-09-18 04:14:41

标签: iphone ios uiview uiviewcontroller uinavigationcontroller

我有一个UINavigationController应用程序,用户可以在多个导航视图上进行多项选择。然后在最终视图中,我允许用户查看他们从先前视图中选择的信息。此信息显示在NavigationController堆栈的最终视图的子视图中。

示例:

UINavigationController
- Several Views including (final view)
Final View 
- several Subviews

当最终的View加载时,我创建了一个执行didRotate方法的deviceOrientation监听器。

[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];

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

然后我调用一个名为currentView的方法,用于确定当前可见的子视图。即当我有类似[finalview.view addSubView:firstView.view]

之类的东西时调用它
[self copyCurrentView:@"firstView"];


- (void)copyCurrentView:(NSString *)currentView {

    myCurrentView = currentView;

}

现在,当旋转设备时,会触发此方法

-(void)didRotate:(NSNotification *)notification
{
    UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];

    if (orientation == UIDeviceOrientationLandscapeLeft)
    {
        if ([myCurrentView isEqualToString:@"firstView"]) {

           [self.navigationController.view insertSubview:firstView.view aboveSubview:self.navigationController.navigationBar];
           [self.navigationController  setNavigationBarHidden:YES animated:YES];

           [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight animated:NO];
           [firstView.view setBounds:CGRectMake(0, 0, 480.0, 320.0)];
           [firstView.view setCenter:CGPointMake(320/2, 480/2)];
           [firstView.view setTransform:CGAffineTransformMakeRotation(M_PI / 2)];
        }
     //other if statments here


    }
    else if (orientation == UIDeviceOrientationLandscapeRight)
    {
      // do the same here
    }
    else if (orientation == UIDeviceOrientationPortrait || orientation == UIDeviceOrientationPortraitUpsideDown)
    {
        NSLog(@"@port");

     if ([myCurrentView isEqualToString:@"firstView"]) {
        [self.view insertSubview:firstView.view belowSubview:actionTabBar];
        [self.navigationController  setNavigationBarHidden:NO animated:YES];



        [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight animated:NO];
        [firstView.view setBounds:CGRectMake(0.0, infoBarHeight, firstView.view.bounds.size.width, firstView.view.bounds.size.height)];
//        [firstView.view setCenter:CGPointMake(480/2, 320/2)];
        [firstView.view setTransform:CGAffineTransformMakeRotation(M_PI * 2)];
      }
    }
}

现在这看起来有点杂乱,可能是一种非常随意的做法..然而,从我在网上发现的一切,我无法弄清楚如何获得良好的效果。

目前当我旋转设备时,它完全全屏显示..完全像我想要的 然而,当我再次旋转到肖像时,它永远不会回到原始位置,即使我使用正确的尺寸等。

如果有人可以帮我设置旋转的MODEL视图,那么当前可见的子视图会非常有用。

我一直犹豫要问这个问题对我来说很难以理解的方式解释......但我已尽力解释我所做的事情以及我需要尽可能多的帮助。希望有人可以帮助我。

问候

1 个答案:

答案 0 :(得分:3)

这最终对我有用,这是全屏旋转,希望它能帮到你:

- (void)viewDidLoad
{
    [super viewDidLoad];
    isShowingLandscapeView = NO;
    [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(orientationChanged:)
                                                 name:UIDeviceOrientationDidChangeNotification
                                               object:nil];
    // Do any additional setup after loading the view.
}

- (void)orientationChanged:(NSNotification *)notification
{
    UIDeviceOrientation deviceOrientation = [UIDevice currentDevice].orientation;
    CGRect rect = CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.height, [[UIScreen mainScreen] bounds].size.width);;


    switch (deviceOrientation) {
        case 1:
            [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait animated:NO];
            [UIView beginAnimations:nil context:NULL];
            [UIView setAnimationDuration:0.1];
            self.view.transform = CGAffineTransformMakeRotation(0);
            self.view.bounds = [[UIScreen mainScreen] bounds];
            [UIView commitAnimations];
            break;
        case 2:
            [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortraitUpsideDown animated:NO];
            [UIView beginAnimations:nil context:NULL];
            [UIView setAnimationDuration:0.1];
            self.view.transform = CGAffineTransformMakeRotation(-M_PI);
            self.view.bounds = [[UIScreen mainScreen] bounds];
            [UIView commitAnimations];
            break;
        case 3:
            [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight animated:NO];
            //rect = CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.height, [[UIScreen mainScreen] bounds].size.width);
            [UIView beginAnimations:nil context:NULL];
            [UIView setAnimationDuration:0.1];
            self.view.transform = CGAffineTransformMakeRotation(M_PI_2);
            self.view.bounds = rect;
            [UIView commitAnimations];
            break;
        case 4:
            [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeLeft animated:NO];
            [UIView beginAnimations:nil context:NULL];
            [UIView setAnimationDuration:0.1];
            //rect = CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.height, [[UIScreen mainScreen] bounds].size.width);
            self.view.transform = CGAffineTransformMakeRotation(-M_PI_2);
            self.view.bounds = rect;
            [UIView commitAnimations];
            break;

        default:
            break;
    }
}