UIView在重新访问时很少被轮换的可能原因是什么?

时间:2009-08-06 02:44:45

标签: iphone uiviewcontroller rotation nib landscape

我将此代码放入我的所有viewControllers:

    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上。在模拟器上,我记得它永远不会发生。你知道这可能的原因是什么吗?提前谢谢。

1 个答案:

答案 0 :(得分: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];
}

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