CABasicAnimation具有5张图像

| 如何更改此代码以使动画如下所示: 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\"];
    
已邀请:
        最好是迟来的答案,然后我想没人。 :) 您的代码中有几个错误。 首先,如果要使用图像的淡入淡出功能,则应将动画添加到包含图像的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];
希望这可以帮助。 小牛     

要回复问题请先登录注册