Question

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

I used above code to set self.view.backgroundColor Image when device is oriented to either Landscape or Portrait. The Images changing correctly.

Problem:

  1. When i try to rotate the device the images is not changing quickly, I mean it taking 2 to 3 seconds to change the image.

  2. How to set self.view.backgroundColor image for the first time depending upon Landscape or Portrait.

Was it helpful?

Solution

Please try the following:

Answer 1: Do it in willRotateToInterfaceOrientation:duration: method of your VC. This method is the animation method that is called when interface orientation changes and would give a smooth transition between background images.

Answer 2: Set your self.view.backgroundColor with required image, in viewWillAppear:animated: method taking self.interfaceOrientation into account, which reflects the current interface orientation of the VC.

Ideally you can create a method like this:

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

Now call this method from either place:

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

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

    [self setBackgroundForInterfaceOrientation:self.interfaceOrientation];
}

Hope it helps.

OTHER TIPS

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

Use it

     -(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"]];
          }
       }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top