isaacselement
isaacselement

Reputation: 2689

UIImageView change Background Color and setImage not working during Animation

As the title said, it's very strange, did you agree?

So , if someone found your codes

imageView.backgroundColor = [UIColor greenColor]; 

or

imageView.image = [UIImage imageName:@"angryBird"];" 

are not working. Just mind that if your imageView is animating , or whether the animation has been removed or not.

----------The Answer Below---------------

Just stop animating before you set image and change background of the animating UIImageView.

UIImageView* imageView = [UIImageView alloc] init];
//......do sth. like setFrame ...
imageView.images = [NSArray arrayWithObjects....]; // set the animation images array 
imageView.animationDuration = 1.0;
[imageView startAnimating];
//......do sth.
[imageView stopAnimating];        // **** do not forget this!!!!! ****
imageView.backgroundColor = [UIColor greenColor];
imageView.image = [UIImage imageName:@"angryBird"];

When you performSelector: withObject: afterDey:1.0s , in the selector method , also, you need to stopAnimating too, and then setImage or change background color.

Upvotes: 5

Views: 17160

Answers (4)

chandan
chandan

Reputation: 2453

Try this code and performSelector:withObject:afterDelay:: in this way

    [self performSelector:@selector(animationDidFinish:) withObject:nil
         afterDelay:instructions.animationDuration];

or use dispatch_after:

    dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, instructions.animationDuration * NSEC_PER_SEC);
    dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
         [self animationDidFinish];
    });

i hope it helps you

Upvotes: 0

Lithu T.V
Lithu T.V

Reputation: 20021

Try this,Animation using block

[UIView animateWithDuration:1.0 animations:^{
    }
    completion:^(BOOL finished)
    {
        [imageView setImage:[UIImage imageNamed:@"YOUR_IMAGE_NAME"]];
        [imageView setBackgroundColor:[UIColor greenColor]];


    }];

Note: It is imageNamed: not imageName: ,I am not sure how this get compiled

Upvotes: 0

Vinod Jat
Vinod Jat

Reputation: 1374

Try to change background color and changing the image once animation is complete. it will simply work for you.

Upvotes: 5

Leta0n
Leta0n

Reputation: 571

Try this:

[UIView animateWithDuration:1.0 animations:^(){} completion:^(BOOL finished){}];

Upvotes: 1

Related Questions