Changing UIImageView Background color and setImage do not work during animation

As the name says, this is very strange, did you agree?

So, if someone found your codes

imageView.backgroundColor = [UIColor greenColor]; 

or

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

does not work. Keep in mind that if your ImageView is animating or the animation is deleted or not.

---------- Answer below ---------------

Just stop the animation before setting the image and change the background of the animated 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 execute Selector: withObject: afterDey: 1.0s, you must also stopAnimating in the selector method and then setImage or change the background color.

+4
source share
4 answers

Try changing the background color and changing the image after the animation is complete. It will just work for you.

+3
source

Try the following:

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

Try this code and make a selection: withObject: afterDelay :: 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]; }); 

Hope this helps you.

0
source

Try Block Animation

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

Note: imageNamed: not imageName: I'm not sure how this compiled

0
source

Source: https://habr.com/ru/post/1483772/


All Articles