Is there a documented way to set the iPhone orientation?

后端 未结 17 1070
清歌不尽
清歌不尽 2020-11-22 11:09

I have an app where I would like to support device rotation in certain views but other don\'t particularly make sense in Landscape mode, so as I swapping the views out I wou

17条回答
  •  长发绾君心
    2020-11-22 11:56

    I solved this quite easily in the end. I tried every suggestion above and still came up short, so this was my solution:

    In the ViewController that needs to remain Landscape (Left or Right), I listen for orientation changes:

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

    Then in didRotate:

    - (void) didRotate:(NSNotification *)notification
    {   if (orientationa == UIDeviceOrientationPortrait) 
        {
            if (hasRotated == NO) 
            {
                NSLog(@"Rotating to portait");
                hasRotated = YES;
                [UIView beginAnimations: @"" context:nil];
                [UIView setAnimationDuration: 0];
                self.view.transform = CGAffineTransformIdentity;
                self.view.transform = CGAffineTransformMakeRotation(DEGREES_TO_RADIANS(-90));
                self.view.bounds = CGRectMake(0.0f, 0.0f, 480.0f, 320.0f);
                self.view.frame = CGRectMake(0.0f, 0.0f, 480.0f, 320.0f);
                [UIView commitAnimations];
    
        }
    }
    else if (UIDeviceOrientationIsLandscape( orientationa))
    {
        if (hasRotated) 
        {
            NSLog(@"Rotating to lands");
            hasRotated = NO;
            [UIView beginAnimations: @"" context:nil];
            [UIView setAnimationDuration: 0];
            self.view.transform = CGAffineTransformIdentity;
            self.view.transform = CGAffineTransformMakeRotation(DEGREES_TO_RADIANS(0));
            self.view.bounds = CGRectMake(0.0f, 0.0f, 320.0f, 480.0f);
            self.view.frame = CGRectMake(0.0f, 0.0f, 320.0f, 480.0f);
            [UIView commitAnimations];
    
        }
    }
    

    Keep in mind any Super Views/Subviews that use autoresizing, as the view.bounds/frame are being reset explicitly...

    The only caveat to this method for keeping the view Landscape, is the inherent animation switching between orientations that has to occur, when it would be better to have it appear to have no change.

提交回复
热议问题