How to make images appear and disappear in iOS apps?

I'm trying to make my application show a picture (PNG) in the middle of the screen when I click a button , and then make it fade after a few seconds. How can I do this? Also, this is a small png, so how can I just show it in its original size and then stretch it to fit the entire screen? Any advice, suggestion or answer is welcome!

I am also new to this site, so could you please try me out or help me improve this question , as some people think this is not complete. Thank you all for your generous answers! :)

+4
source share
3 answers

Initialize UIImageView with UIImage :

 // Assuming MyImage.png is part of the project resources. UIImage *image = [UIImage imageNamed:@"MyImage.png"]; UIImageView *imageView = [[UIImageView alloc] initWithImage:image]; // Add imageView to a parent view here. [UIView animateWithDuration:0.2f delay:3.0f options:0 animations:^{imageView.alpha = 0.0;} completion:^{[imageView removeFromSuperview];}]; 
+10
source
  NSArray *animationArray = [NSArray arrayWithObjects:[UIImage imageNamed:@"myImage.png"], nil]; [NSTimer scheduledTimerWithTimeInterval:.75 target:self selector:@selector(crossfade) userInfo:nil repeats:YES]; mainImageView.animationImages = animationArray; mainImageView.animationDuration = 4.5; //mainImageView is instance of UIImageView mainImageView.animationRepeatCount = 0; [mainImageView startAnimating]; CABasicAnimation *crossFade = [CABasicAnimation animationWithKeyPath:@"contents"]; crossFade.autoreverses = YES; crossFade.repeatCount = 1; crossFade.duration = 1.0; 

and target method:

 - (void) crossfade { [UIView beginAnimations:nil context:nil]; [UIView setAnimationDuration:0.5]; [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut]; // user dependent transition acn be set here mainImageView.alpha = !mainImageView.alpha; [UIView commitAnimations]; } 
+3
source

Here is the code to display the image for a few seconds when the button is pressed: in the button action, add the following code:

 imageView.image=yourImage; [self performSelector:@selector(waitAndGo) withObject:nil afterDelay:5]; 

here is the implementation for waitAndGo:

 -(void)waitAndGo { imageView.hidden=YES; } 
+3
source

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


All Articles