2014-02-12 45 views
1

我试图做两个图像之间的交叉淡入淡出,但是当您调用函数来交换图片时,一切都是白色的,并在第二个图像后不久出现,并且不会发生转换。我究竟做错了什么?Crossfade图像与CABasicAnimation不工作ios

感谢

我的代码:

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

回答

0

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

+2

这不是一个好的答案。请解释您的设备出现了什么问题,以及为什么它会停止您的动画工作。 – TigerCoding