CABasicAnimation 包含 5 个图像

发布于 2024-11-15 19:38:19 字数 954 浏览 3 评论 0原文

如何更改此代码以使动画如下所示: 图片1 -->图片2 ---> 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"];

how to change this code to make the animation like this:
image1 --> image2 ---> image3 --->image4 --->image 5 .. then return back to image1 and so on ...

the code:

    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"];

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

诗化ㄋ丶相逢 2024-11-22 19:38:19

我认为迟到的答案比没有人更好。 :)

您的代码中有几个错误。

首先,如果你想使用图像交叉淡入淡出,你应该将动画添加到包含图像的 imageView 中,然后在 UIImage 之间切换。

第二:如果你想循环浏览两个以上的图像,你应该在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];

希望这有帮助。
飞行器

Better a late answer then no one i think. :)

There are several mistakes in your code.

First of all if you want to use crossfade of images you should add the animation to the imageView containing the images and just switch between the UIImages.

Second: If you want to cycle through more images than two, you should either use a queue of animations with the help of animationDidFinish callback methods, or you should use the animationImages field of the UIImageView class to let the UIImageView do all the work.

Third: If you are changing the layer of the image from image1 to image5 how could you expect the compiler to know that it should put all the other pics in between?

Try this code:

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

hope this helps.
Mav

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文