UIImageView change Background Color and setImage not working during Animation

16,659

Solution 1

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

Solution 2

Try this:

[UIView animateWithDuration:1.0 animations:^(){} completion:^(BOOL finished){}];
Share:
16,659
isaacselement
Author by

isaacselement

Coder in the wild .

Updated on June 04, 2022

Comments

  • isaacselement
    isaacselement almost 2 years

    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.

  • chandan
    chandan almost 11 years
    Before assigning image name as angryBird please double check your spell
  • isaacselement
    isaacselement almost 11 years
    thanks. It's not the frame's problem . I change imageView's background color before its animation stop, and the rigth way is that I should change the background color after its animation stop . Or, another way , stop its animation and then , change its background color.
  • isaacselement
    isaacselement almost 11 years
    ;) , sorry ,forget about the "imageName:" and "angry bird", I wat not coding it in XCode. i just write it in this textArea.
  • chandan
    chandan almost 11 years
    so what u really want. Do u want to change background color after animation stop or before animation stop ?
  • isaacselement
    isaacselement almost 11 years
    yes : ) , its animation must stop , and change background color would work.
  • isaacselement
    isaacselement almost 11 years
    His thought is right. and , what i really want is to point out that : if someone want to change the UIImageView background color or image , he should stop its animating. put the code "[imageView stopAnimating]" before "imageView.backgroundColor = [UIColor greenColor]" . if not , set BGColor would failed. Many thanks @chandan, :)