开发者

CABasicAnimation with 5 images

开发者 https://www.devze.com 2023-03-13 16:44 出处:网络
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 ...

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;

[sel开发者_如何学Pythonf.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"];


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号