将屏幕从纵向旋转到横向

时间:2011-08-19 04:41:09

标签: iphone rotation

我使用网络上的以下代码将屏幕旋转到横向模式。我不明白他们想做什么。特别是它设置的界限。有人能解释一下它在做什么吗?

if ([[UIApplication sharedApplication] statusBarOrientation] == UIInterfaceOrientationPortrait)
{
    CGRect statusBarFrame = [[UIApplication sharedApplication] statusBarFrame];

    [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight animated:NO];


    UIScreen *screen = [UIScreen mainScreen];
    CGRect newBounds = CGRectMake(0, 0, screen.bounds.size.height, screen.bounds.size.width - statusBarFrame.size.height);



    self.navigationController.view.bounds = newBounds;
    self.navigationController.view.center = CGPointMake(newBounds.size.height / 2.0, newBounds.size.width / 2.0);
    self.navigationController.view.transform = CGAffineTransformConcat(self.navigationController.view.transform, CGAffineTransformMakeRotation(degreesToRadian(90)));
    self.navigationController.view.center = window.center;
}

3 个答案:

答案 0 :(得分:0)

你的rootView的大小曾经是(320,480),例如,在旋转之后,你应该将它设置为(480,320)以便在横向模式下适应屏幕,这就是你需要改变你的界限的原因图。

设置变换使视图实际旋转90度。

UIKit在为你自动旋转时会做类似的事情。

答案 1 :(得分:0)

Hello Janaka,

         You will try this code.

         Take a look at the function `shouldAutorotateToInterfaceOrientation`: in the UIViewController class. This function returns YES if the orientations is supported by your UIView. If you return YES only to the landscape orientation, then the iPhone will automatically be put in that orientation.

以下代码应该这样做。将它放在UIViewController中,该控件控制您想要以横向模式放置的视图。

使用此方法。 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {     //返回YES表示支持的方向     return(interfaceOrientation == UIInterfaceOrientationLandscape); }

试试这个链接肯定会帮到你 this

答案 2 :(得分:0)

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

- (void)viewWillAppear:(BOOL)animated
{

    [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight];

    CGRect newBounds = CGRectMake(0, 0, 480, 320);
    self.navigationController.view.bounds = newBounds;
    self.navigationController.view.center = CGPointMake(newBounds.size.height / 2.0, newBounds.size.width / 2.0);

    self.navigationController.view.transform = CGAffineTransformMakeRotation(degreesToRadians(90));

    [super viewWillAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated
{
    self.navigationController.view.transform = CGAffineTransformIdentity;
    self.navigationController.view.transform = CGAffineTransformMakeRotation(degreesToRadians(0));
    self.navigationController.view.bounds = CGRectMake(0.0, 0.0, 320.0, 480.0);

    [super viewWillDisappear:animated];
}
相关问题