2011-06-18 81 views
0

如何更改此代碼以使動畫如下: image1 - > image2 - > image3 - > image4 - > image 5 ..然後返回到image1等上...CABasicAnimation with 5 images

代碼:

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

回答

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

希望這會有所幫助。 Mav