简体   繁体   中英

How to rotate and resize UIView on device rotation

I have a UINavigationController application where the user makes several selections on several navigation views. then on the final view I allow the user to see the information that was defined by their selections from the previous views. This information is presented in subviews on the finalview of the NavigationController stack.

example:

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

When The final View loads I create a deviceOrientation listener which executed the didRotate method.

[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];

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

I then call a method called currentView which is used to figure out which subview is currently visible. ie this is called when ever i have something like a [finalview.view addSubView:firstView.view]

[self copyCurrentView:@"firstView"];


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

    myCurrentView = currentView;

}

Now when the device is rotated this method is triggered

-(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)];
      }
    }
}

Now this looks a little scrappy and probably a really random way of doing it.. however from everything I Have found online i cannot figure out how to get a good effect.

currently when i rotate the device left it goes full screen perfectly.. exactly like i want HOWEVER when i rotate to portrait again it never goes back to the original position even though I am using the correct sizing etc.

if anyone could help me set up a MODEL view on rotation for what ever subview is currently visible that would be greatly appreciate.

I have been hesitant to ask this question as to me its very hard to explain in an understanding manner.. but I Have done my best to explain what I Have done and what I need help with as best as I can.. Hopefully someone can help me.

regards

This is what finally worked for me, this is doing a full screen rotation, hope it will help you:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM