简体   繁体   中英

UIImage Cross Dissolve not animating using UIView transitionWithView

I am attempting to cross-fade from one UIImage to another UIImage which are contained in a UIImageView. This seems like it should be accomplished with UIView Animation with the following code:

#import "ViewController.h"

@interface ViewController ()

@property (nonatomic, strong) IBOutlet UIImageView *sunImageView;

@end

@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];

_sunImageView.image = [UIImage imageNamed:@"sun1.png"];

UIImage *image = [UIImage imageNamed:@"sun3.png"];
[UIView transitionWithView:self.view
                  duration:3.0f
                   options:UIViewAnimationOptionTransitionCrossDissolve
                animations:^{
                    _sunImageView.image = image;
                    //  _sunImageView.alpha = 0;
                } completion:^(BOOL finished){
                    NSLog(@"FINISHED");
                }];
}

The IBOutlet is wired up correctly as the commented out code which animates the .image alpha from 1 to 0 works fine. When this code runs, sun3.png is the image which is presented. sun1.png, even though it is set prior to the animation is never displayed.

For some reason, this code does not transition from sun1.png to sun3.png. I have searched quite a bit and this is the suggested solution for cross-dissolving images in an image view, but it doesn't seem to be working for me. Any thoughts would be appreciated.

尝试将动画代码放入viewDidAppear:方法中。

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