我正在尝试在两个图像之间进行交叉淡入淡出,但是当您调用该函数交换图片时,一切都是白色的,并且在第二张图像之后很快出现并且不会发生过渡。我究竟做错了什么?
谢谢
我的代码:
#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];
}