0

当方向改变时,我想改变我的视图控制器的背景图片。我在 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];
}

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

4

1 回答 1

1

我猜,但你可能太早更新你的观点。而不是调用您的方法shouldAutorotateToInterfaceOrientation,您是否尝试过这样做didRotateFromInterfaceOrientation

即进行此更改:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return YES;
}
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
    [self setupGUIForOrientation:self.interfaceOrientation];
}
于 2012-04-13T14:18:30.543 回答