iPhone SDK: Orientation (Landscape and Portrait views)

后端 未结 5 1984
醉梦人生
醉梦人生 2020-12-02 19:38

Okay, I\'ve got my normal app which is in portrait mode. I can force my app to go to landscape mode for a view (using navigationcontroller and viewcontroller) like this:

5条回答
  •  渐次进展
    2020-12-02 20:11

    Here's what I'm doing to do this:

    first, put this define at the top of your file, right under your #imports:

    #define degreesToRadian(x) (M_PI * (x) / 180.0)
    

    then, in the viewWillAppear: method

    [[UIApplication sharedApplication] setStatusBarHidden:YES animated:NO];     
    if (self.interfaceOrientation == UIInterfaceOrientationPortrait) {  
        self.view.transform = CGAffineTransformIdentity;
        self.view.transform = CGAffineTransformMakeRotation(degreesToRadian(90));
        self.view.bounds = CGRectMake(0.0, 0.0, 480, 320);
    }
    

    if you want that to be animated, then you can wrap the whole thing in an animation block, like so:

    [UIView beginAnimations:@"View Flip" context:nil];
    [UIView setAnimationDuration:1.25];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
    
    [[UIApplication sharedApplication] setStatusBarHidden:YES animated:NO];     
    if (self.interfaceOrientation == UIInterfaceOrientationPortrait) {  
        self.view.transform = CGAffineTransformIdentity;
        self.view.transform = CGAffineTransformMakeRotation(degreesToRadian(90));
        self.view.bounds = CGRectMake(0.0, 0.0, 480, 320);
    }
    [UIView commitAnimations];
    

    Then, in your portrait mode controller, you can do the reverse - check to see if its currently in landscape, and if so, rotate it back to Portrait.

提交回复
热议问题