重新访问时 UIView 很少不旋转的可能原因有哪些?

发布于 2024-07-30 02:04:51 字数 1344 浏览 1 评论 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上出现的频率更高。 在模拟器上,我记得这从未发生过。 您知道造成这种情况的可能原因有哪些吗? 先感谢您。

I put this code into all of my 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;
    }   

and I also have:

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

    return (interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}

I can see that the codes work correctly as they should, but once in a while, there is a rare occasion that the main view controller failed to rotate for landscape mode after I put its view back to be the main view. The chance that it occurs is very rare, but this is nagging me. Moreover, I can see that it occurs more often on the iphone. On the simulator, I recalled it never happen. Do you know what are the possible causes for this? Thank you in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

々眼睛长脚气 2024-08-06 02:04:51

我找到了我的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];
}

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

I found the cause of my iphone program.

One of the possible causes is memory. This method below will be called when the system is low on memory:

- (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.
}

An "unseen" UIView is unloaded when this method is called. When you are about to see this view again, the device reloaded it, but maybe because you set the parameters (for the rotation, etc.) in a place where it won't be called when it gets reloaded. You may use:

- (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];
}

It occurs in the device because of we are talking real memory on the real device. You can use "Hardware > Simulate Memory Warning" when you are using the simulator.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文