简体   繁体   中英

iOS: How to change an UIImageView by pressing a UIButton

I have an UIImageView and an UIButton in my app. What I want to do is that when the user taps the UIButton, the UIImageView will change to another image and then almost instantly go back to the original image. To simulate an animation.

The code:

file.h

{

IBOutlet UIImageView *image1;
IBOutlet UIButton *button;

}

-(IBAction)button:(id)sender;

file.m

-(IBAction)button:(id)sender{


}

What do I have to implement in both files to make this happen?

Any help would be greatly appreciated. Thank you in advance!

Sure.

Insert the following code inside "-(IBAction)button:(id)sender":

[UIView animateWithDuration:0.5f
                 animations:^{

                     image1.alpha = 0.1f;

                 } completion:^(BOOL finished) {

                     image1.image = [UIImage imageNamed:@"image2"]; // @"image2.png"

                     [UIView animateWithDuration:1.0f
                                      animations:^{

                                          image1.alpha = 1.0f;

                                      } completion:^(BOOL finished) {

                                          image1.image = [UIImage imageNamed:@"image1"];

                                      }];

                 }];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM