objective c - Background Colour Continuous Change - iOS


Keywords:ios 


Question: 

How to make UIView background colour continuous changing like an Instagram log-in screen has as shown in the images without load in the memory.

enter image description here enter image description here


1 Answer: 

To change the background color you just do:

    self.view.backgroundColor=yourColor;

and to change this continuously you can use a timer.IF you have a an array colors for the background, store the colors in an array.

   NSArray *colorArray=@[colo1,color2....colorn];

You can also generate a random color and pass it to the view background.In that case you can just do(In this case, you don't have to store the colors in an array)

      self.view.backgroundColor=metodThatReturnsRandomColor.

OR, declare the timer to call the changeColor method every 1.0 sec .

  @property (nonatomic, retain) NSTimer * timerIvar;


 self.timerIvar  = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(changeColor) userInfo:nil repeats:YES];

and the method would be:

-(void)changeColor{
         if(x<colorArray.count){
        self.view.backgroundColor=colorArray[x];
        }else{
          x=0;
       }
        x++;
  }

The same logic applies for images/ gradient colors.

and remember on viewWillDisappear

- (void)viewWillDisappear:(BOOL)animated
{
  ...
  [self.timerIvar invalidate];
 self.timerIvar = nil;
}