当方向改变时,我想更改视图控制器的背景图片。我在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];
}
一切都很好(图像和框架改变)除了一件事:当旋转发生时,我可以看到视图控制器的背景颜色一秒钟,这真的很难看。我可以以某种方式阻止它吗?而且,我可以更好地编写此功能吗?谢谢你的帮助!
答案 0 :(得分:1)
我猜,但你可能会过早更新你的观点。不是在shouldAutorotateToInterfaceOrientation
中调用您的方法,您是否尝试在didRotateFromInterfaceOrientation
中执行此操作?
即。做出这样的改变:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return YES;
}
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
[self setupGUIForOrientation:self.interfaceOrientation];
}