-1

I'm animating some subviews, and one of the steps of the animation is to change the view background color. But, despite I have a function that returns a random color, once the background color is set, during the animation definition, it doesn't call my function again to get another color.

Is there some way I can force the animation to call my function every time the animation is executed?


Snippet:

for (UIView* subView in self.view.subviews) {
    dispatch_async(dispatch_get_main_queue(), ^{
                    [UIView animateWithDuration:1.0f
                          delay:0.0f
                        options: UIViewAnimationOptionRepeat
                     animations:^{
                         subView.backgroundColor = getRandomColor();
                     }
                     completion:nil];
                     });
}
Mauricio Zambon
  • 695
  • 2
  • 9
  • 17
  • 1
    can you show us the code you are currently trying? – JoeCortopassi Mar 23 '13 at 17:19
  • The thing is that, once the color is set for the animation, when it repeats the animation, it doesn't call my function ever again to get another color. I want that every time the animation repeats, it repeats with a different color. – Mauricio Zambon Mar 23 '13 at 17:37
  • Can you try assigning the output of getRandomColor() into a UIColor variable outside of the UIView animation block, and then set the background color to that variable inside the animation block? – Jai Govindani Mar 23 '13 at 18:32
  • According to this: http://stackoverflow.com/questions/3762561/how-to-animate-the-background-color-of-a-uilabel - try playing with subview.layer instead of subview. It seems to be working that way, if GCD is not playing any foul. – Nirav Bhatt Mar 23 '13 at 18:47
  • It's not working yet, even animating the layer instead of the UIView itself. – Mauricio Zambon Mar 23 '13 at 20:07

2 Answers2

2

getRandomColor(); will only get called once and subView will animate its background color to that color every 1 second (so it will animate to the color from your original background color and then animate to the same color every 1 second).

What you need to do is put this in a method, remove UIViewAnimationOptionRepeat, and add a completion block to the UIView animation that calls the method again.

fumoboy007
  • 5,345
  • 4
  • 32
  • 49
-2

If all you want is toggling background color, here is an alternate approach:

  • No need to do for loop
  • No need to do dispatch_async

    NSArray *animationImages = [[NSArray alloc] initWithObjects:
    [UIImage imageNamed:@"image1.png"],    
    [UIImage imageNamed:@"image2.png"],
        ....
    [UIImage imageNamed:@"imagen.png"], nil];
    
    self.imageView.animationImages=animationImages;
    self.imageView.animationDuration=1.0;  //set duration
    [self.imageView startAnimating];
    

Where each image1..imageN.png is made of the color you desire, imageView is your placeholder view of background color.

Disclaimer: It is possible there is better solution available with backgroundcolor as animatable property itself. This is just to save you from unknowns of GCD in case you might have used it without knowing why you are using it the way you do.

Nirav Bhatt
  • 6,940
  • 5
  • 45
  • 89
  • 1
    This is a terrible solution. You don't use an image to do coloring, just no. – Richard J. Ross III Mar 23 '13 at 20:12
  • A solution can be good or bad based on need."You don't use" - Declarative statement. How about "You just don't dig foundation with a spoon?" Classically perfect things don't always make a good solution. Not always, yes. Besides, declarative statements do not add any quality to further answers. If you can, enlighten me with something really good, I am all ears for it. – Nirav Bhatt Mar 24 '13 at 00:42