【问题标题】:Change different self.view.backgroundColor Images When app Oriented to Landscape and Portrait当应用程序面向横向和纵向时更改不同的 self.view.backgroundColor 图像
【发布时间】:2014-04-03 11:59:23
【问题描述】:
- (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图像。

【问题讨论】:

  • 做个猜测:尝试使用 -(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration 而不是 didRotate 让我知道它是否有效 :)
  • 使用我的所有三种方法,你得到了解决方案

标签: ios objective-c orientation autolayout


【解决方案1】:

请尝试以下方法:

答案 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];
}

希望对你有帮助。

【讨论】:

  • willRotateToInterfaceOrientation 方法比使用的要好。谢谢帮助。
  • @user1997951 很高兴它有帮助。如果您已收到解决方案,请将其标记为答案。
【解决方案2】:
- (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"]];
    }
}

【讨论】:

    【解决方案3】:

    使用它

         -(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"]];
              }
           }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-06-12
      • 1970-01-01
      • 2014-02-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-12
      相关资源
      最近更新 更多