当app面向Landscape和Portrait时,更改不同的self.view.backgroundColor图像

时间:2014-04-03 11:59:24

标签: ios objective-c orientation autolayout

- (void) didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation{
    if ((fromInterfaceOrientation == UIInterfaceOrientationPortrait) || (fromInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)) {
        self.view.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageNamed:@"bgImage.png"]];

    }
    else
    {
        self.view.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageNamed:@"bgImage1.png"]];
    }
}

当设备面向横向或纵向时,我使用上面的代码设置self.view.backgroundColor图像。图像正确更改。

问题:

  1. 当我尝试旋转设备时,图像没有快速变化,我的意思是需要2到3秒才能更改图像。

  2. 如何首次设置self.view.backgroundColor图像,具体取决于横向或纵向。

3 个答案:

答案 0 :(得分:2)

请尝试以下方法:

答案1:在VC的willRotateToInterfaceOrientation:duration:方法中执行此操作。此方法是在界面方向更改时调用的动画方法,可以在背景图像之间进行平滑过渡。

答案2:使用self.view.backgroundColor方法考虑viewWillAppear:animated:,将self.interfaceOrientation与所需图片一起设置,这反映了VC的当前界面方向。

理想情况下,您可以创建一个这样的方法:

-(void)setBackgroundForInterfaceOrientation:(UIInterfaceOrientation)orientation {
    if (UIInterfaceOrientationIsLandscape(orientation)) {
        self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"bg-iphone3-landscape"]];
    }
    else {
        self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"bg-iphone3"]];
    }
}

现在从任何一个地方调用此方法:

-(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
    [self setBackgroundForInterfaceOrientation:toInterfaceOrientation];
}

-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];

    [self setBackgroundForInterfaceOrientation:self.interfaceOrientation];
}

希望它有所帮助。

答案 1 :(得分:1)

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    UIDeviceOrientation currentOrientation = [UIApplication sharedApplication].statusBarOrientation;

    if (currentOrientation == UIInterfaceOrientationLandscapeLeft
        ||  currentOrientation == UIInterfaceOrientationLandscapeRight)
    {
        [imageview setImage:[UIImage imageNamed:@"backgroundImage.png"]];
    }
    else
    {
        [imageview setImage:[UIImage imageNamed:@"Portrait-backgroundImage.png"]];
    }
}

答案 2 :(得分:1)

使用它

     -(void)viewWillAppear:(BOOL)animated
       {

          [self willAnimateRotationToInterfaceOrientation:[UIApplication sharedApplication].statusBarOrientation duration:1.0];

        }


     -(void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
                                     duration:(NSTimeInterval)duration
      {

        if (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft || toInterfaceOrientation == UIInterfaceOrientationLandscapeRight)
          {
              self.view.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageNamed:@"bgImage1.png"]];
          }
        else if (toInterfaceOrientation == UIInterfaceOrientationPortrait || (toInterfaceOrientation==UIInterfaceOrientationPortraitUpsideDown))
          {
             self.view.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageNamed:@"bgImage.png"]];
          }
       }