0

我将此代码放入我所有的视图控制器中:

    UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];

    if (orientation == UIInterfaceOrientationLandscapeRight) {
        CGAffineTransform transform = self.view.transform;

        // Use the status bar frame to determine the center point of the window's content area.
        //CGRect statusBarFrame = [[UIApplication sharedApplication] statusBarFrame];
        //CGRect bounds = CGRectMake(0, 0, statusBarFrame.size.height, statusBarFrame.origin.x);
        CGPoint center = CGPointMake(320/2, 480/2);

        // Set the center point of the view to the center point of the window's content area.
        self.view.center = center;

        // Rotate the view 90 degrees around its new center point.
        transform = CGAffineTransformRotate(transform, (M_PI / 2.0));
        self.view.transform = transform;
    }   

我也有:

 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations

    return (interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}

我可以看到代码可以正常工作,但是偶尔,在我将其视图放回主视图后,主视图控制器很少旋转到横向模式。它发生的机会非常罕见,但这让我很烦。此外,我可以看到它在 iphone 上发生的频率更高。在模拟器上,我记得它从未发生过。您知道造成这种情况的可能原因是什么吗?先感谢您。

4

1 回答 1

0

我找到了我的 iphone 程序的原因。

可能的原因之一是内存。当系统内存不足时,将调用以下此方法:

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

    // Release any cached data, images, etc that aren't in use.
}

调用此方法时会卸载“未见”的 UIView。当您将再次看到此视图时,设备重新加载了它,但可能是因为您将参数(用于旋转等)设置在重新加载时不会被调用的位置。您可以使用:

- (void)viewDidLoad {

    UIApplication* application = [UIApplication sharedApplication];
    application.statusBarOrientation = UIInterfaceOrientationLandscapeRight;

    CGAffineTransform landscapeTransform = CGAffineTransformMakeRotation([MyMath radd:90]);
    landscapeTransform = CGAffineTransformTranslate (landscapeTransform, -80, 83);

    [self.view setTransform: landscapeTransform];
    [super viewDidLoad];
}

它发生在设备中,因为我们正在谈论真实设备上的真实内存。您可以在使用模拟器时使用“硬件 > 模拟内存警告”。

于 2009-08-11T04:19:05.417 回答