我有 7 个图像,我正试图淡入/淡出 1 个 ImageView。我已将所有图像存储在一个数组中,然后有一个循环来显示每个图像并淡入下一个,但是,当我运行程序时,只显示最后 2 个图像。关于为什么会这样的任何想法?以及如何解决?代码如下。
imageArray = [NSArray arrayWithObjects:
[UIImage imageNamed:@"image 0.jpg"],
[UIImage imageNamed:@"image 1.jpg"],
[UIImage imageNamed:@"image 2.jpg"],
[UIImage imageNamed:@"image 3.jpg"],
[UIImage imageNamed:@"image 4.jpg"],
[UIImage imageNamed:@"image 5.jpg"],
[UIImage imageNamed:@"image 6.jpg"],
nil];
self.imageview.backgroundColor = [UIColor blackColor];
self.imageview.clipsToBounds = YES;
int count = [imageArray count];
for (int i = 0; i <count-1 ; i++)
{
UIImage *currentImage = [imageArray objectAtIndex: i];
UIImage *nextImage = [imageArray objectAtIndex: i +1];
self.imageview.image = [imageArray objectAtIndex: i];
[self.view addSubview:self.imageview];
CABasicAnimation *crossFade = [CABasicAnimation animationWithKeyPath:@"contents"];
crossFade.duration = 5.0;
crossFade.fromValue = (__bridge id)(currentImage.CGImage);
crossFade.toValue = (__bridge id)(nextImage.CGImage);
[self.imageview.layer addAnimation:crossFade forKey:@"animateContents"];
self.imageview.image = nextImage;
};