ios - 屏幕方向改变时背景图片为 "blinking"

标签 ios orientation

当方向改变时,我想改变 View Controller 的背景图片。我在 shouldAutorotateToInterfaceOrientation 中调用 setupGUIForOrientation 函数,如下所示:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    [self setupGUIForOrientation:interfaceOrientation];
    return YES;
}

setupGUIForOrientation 函数:

-(void)setupGUIForOrientation:(UIInterfaceOrientation)orientation
{
    if(!background) background = [[UIImageView alloc] init];
    UIImage* image = [[StorageProvider storage].imageProvider getImageForSurfaceElement:@"background"];
    int width = 0;
    int height = 0;
    CGRect bounds = [[UIScreen mainScreen] bounds];
    if(UIInterfaceOrientationIsPortrait(orientation)) {
        width = MIN(bounds.size.width, bounds.size.height);
        height = MAX(bounds.size.width, bounds.size.height);
    } else {
        width = MAX(bounds.size.width, bounds.size.height);
        height = MIN(bounds.size.width, bounds.size.height);
    }
    background.frame = CGRectMake(0, 0, width, height);
    [self.view addSubview: background];
    [self.view sendSubviewToBack: background];
    [background setImage:image];
}

一切都很好(图像和框架发生变化),除了一件事:当旋转发生时,我可以看到 View Controller 的背景颜色一秒钟,这真的很难看。我可以以某种方式阻止它吗?而且,我可以用更好的方式编写这个函数吗?感谢您的帮助!

最佳答案

我猜,但您可能太早更新了您的 View 。您是否尝试过在 didRotateFromInterfaceOrientation 中调用您的方法,而不是在 shouldAutorotateToInterfaceOrientation 中调用您的方法?

即进行此更改:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return YES;
}
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
    [self setupGUIForOrientation:self.interfaceOrientation];
}

关于ios - 屏幕方向改变时背景图片为 "blinking",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10142244/

相关文章:

ios - 无法使用 xcode 9 在 iphone 4s 模拟器上运行 UITest

android - 改变方向时不要改变背景

android - 平板电脑与手机方向限制

ios - 更改方向时 UIScrollView 的问题,iOS

javascript - 在 UIWebView 中设置超链接样式

iOS Facebook LoginButton 约束冲突

ios - 如何设置 NSBatchDeleteRequest 的 NSBatchDeleteResult 类型?

ios - 为什么 SwiftUI 中的 Today App Extension Widget 是白色的?

android - getDefaultDisplay().getRotation();总是返回相同的值

ios - 当方向改变时,如何防止 View Controller 旋转一个 View ?