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にアニメーションを追加し、UIImageを切り替える必要があります。

2番目:2つ以上の画像を循環させる場合は、animationDidFinishコールバックメソッドを使用してアニメーションのキューを使用するか、UIImageViewクラスのanimationImagesフィールドを使用してUIImageViewにすべての作業を行わせる必要があります。

3番目:画像のレイヤーを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

于 2011-07-19T10:51:09.397 に答える