使用CABasicAnimation的交叉淡化图像不起作用

时间:2014-02-12 17:08:40

标签: ios7 uiimageview cabasicanimation cross-fade

我正在尝试在两个图像之间进行交叉淡入淡出,但是当您调用函数来交换图片时,一切都是白色的,并且在第二个图像之后很快就会出现并且不会发生转换。我究竟做错了什么?

感谢

我的代码:

#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];
}

1 个答案:

答案 0 :(得分:0)

这解决了。我的设备出现问题。