0

如何更改此代码以使动画如下所示: image1 --> image2 ---> image3 --->image4 --->image 5 .. 然后返回到 image1 等等...

编码:

    UIImage *image1 = [UIImage imageNamed:@"1.jpg"];
UIImage *image2 = [UIImage imageNamed:@"2.jpg"];
UIImage *image3 = [UIImage imageNamed:@"3.jpg"];
UIImage *image4 = [UIImage imageNamed:@"4.jpg"];
UIImage *image5 = [UIImage imageNamed:@"5.jpg"];

NSArray *imageArray = [NSArray arrayWithObjects:image1.CGImage, image2.CGImage, image3.CGImage, nil];

//self.introImages.image = image1;

[self.view addSubview:self.introImages];


CABasicAnimation *crossFade = [CABasicAnimation animationWithKeyPath:@"contents"];
crossFade.autoreverses = YES;
crossFade.repeatCount = HUGE_VALF;
crossFade.duration = 1.0;
//self.introImages.layer.contents = image2.CGImage;

crossFade.fromValue = (id)image1.CGImage;
crossFade.toValue = (id)image5.CGImage;
[self.introImages.layer addAnimation:crossFade forKey:@"animateContents"];
4

1 回答 1

2

最好是迟到的答案,然后我认为没有人。:)

您的代码中有几个错误。

首先,如果您想使用图像淡入淡出,您应该将动画添加到包含图像的 imageView 并在 UIImages 之间切换。

第二:如果你想循环显示多于两个的图像,你应该在 animationDidFinish 回调方法的帮助下使用动画队列,或者你应该使用 UIImageView 类的 animationImages 字段让 UIImageView 完成所有工作。

第三:如果您将图像的图层从 image1 更改为 image5,您怎么能期望编译器知道它应该将所有其他图片放在两者之间?

试试这个代码:

UIImage *image1 = [UIImage imageNamed:@"1.jpg"];
UIImage *image2 = [UIImage imageNamed:@"2.jpg"];
UIImage *image3 = [UIImage imageNamed:@"3.jpg"];
UIImage *image4 = [UIImage imageNamed:@"4.jpg"];
UIImage *image5 = [UIImage imageNamed:@"5.jpg"];

NSArray *imageArray = [NSArray arrayWithObjects:image1.CGImage, image2.CGImage, image3.CGImage, nil];
//imageView to add the array of images you want to cycle through
iv_introImages.animationImages = imageArray;
//duration of the animation-cycle
iv_introImages.animationDuration = 1.0;
//how often you want the animation to repeat. 0 stands for endless repetition
iv_introImages.animationRepeatCount = 0;
//starts the animation
[iv_introImages startAnimating];

希望这可以帮助。小牛

于 2011-07-19T10:51:09.397 回答