gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 04:42:36 24 4
gpt4 key购买 nike

当方向改变时,我想改变 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com