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){}];
Comments
-
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 almost 11 yearsBefore assigning image name as angryBird please double check your spell
-
isaacselement almost 11 yearsthanks. 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 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 almost 11 yearsso what u really want. Do u want to change background color after animation stop or before animation stop ?
-
isaacselement almost 11 yearsyes : ) , its animation must stop , and change background color would work.
-
isaacselement almost 11 yearsHis 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, :)