Search code examples
ios7uiimageviewcabasicanimationcross-fade

Crossfade image with CABasicAnimation not work ios


I'm trying to do a crossfade between two images, but when you call the function to exchange pictures, everything is white and appears soon after the second image and the transition does not occur. What am I doing wrong?

thanks

My code:

#import <QuartzCore/QuartzCore.h>

#define QT_IMG_HOME 2

@interface UFHomeViewController () {
  int idxImgBG;
}

@end


- (void)viewDidLoad
{
    [super viewDidLoad];
// Do any additional setup after loading the view.
    idxImgBG = 1;
    [self performSelector:@selector(changeBGImage) withObject:nil afterDelay:3.0];
}

-(void)changeBGImage {
    if(idxImgBG < QT_IMG_HOME)
        idxImgBG = idxImgBG + 1;
    else
        idxImgBG = 1;

    UIImage *image1 = _imgBG.image;
    UIImage *image2 = [UIImage imageNamed:[NSString stringWithFormat:@"home%d.jpg", idxImgBG]];

    CABasicAnimation *crossFade = [CABasicAnimation animationWithKeyPath:@"contents"];
    crossFade.duration = 1;
    crossFade.fromValue = (__bridge id)(image1.CGImage);
    crossFade.toValue = (__bridge id)(image2.CGImage);
    [_imgBG.layer addAnimation:crossFade forKey:@"animateContents"];

    _imgBG.image = image2;

    [self performSelector:@selector(changeBGImage) withObject:nil afterDelay:4];
}

Solution

  • This solved. Problems with my device.