如何让图像数组动画在Xcode中只循环一个循环? [重复]

时间:2022-09-26 11:05:51

This question already has an answer here:

这个问题在这里已有答案:

I have an image array animation, however it is a continuous loop. How do I get it to cycle through only one loop and stop on the last frame?

我有一个图像数组动画,但它是一个连续循环。如何让它只循环一个循环并在最后一帧停止?

- (void) balloonAnimation
{
    NSMutableArray *balloonAnimationImages = [NSMutableArray arrayWithCapacity:27];

   for (NSInteger i = 0; i < 27; i++) {
       NSString *nameOfImage = [NSString stringWithFormat:@"balloons%d.png", i+1];
       UIImage *image = [UIImage imageNamed:nameOfImage];
       [balloonAnimationImages addObject:image];       
    }

    //animate here
    self.imgBalloon.animationImages = balloonAnimationImages;
    self.imgBalloon.animationDuration = 5.0f;
    [self.imgBalloon startAnimating];    
}

2 个解决方案

#1


3  

Try this:

self.imgBaloon.animationRepeatCount = 1;

EDIT: As @Elliott Perry stated in his answer, you need to assign the last frame to the image property. Here is the code:

编辑:正如@Elliott Perry在他的回答中所说,你需要将最后一帧分配给image属性。这是代码:

self.imgBalloon.animationImages = balloonAnimationImages;
self.imgBalloon.animationDuration = 5.0f;
self.imgBaloon.animationRepeatCount = 1;
self.imgBaloon.image = [balloonAnimationImages lastObject];
[self.imgBalloon startAnimating]; 

#2


3  

From Can I stop UIImageView Animation at last frame?

从我可以在最后一帧停止UIImageView动画吗?

The image property on the UIImageView class has the following docs: "If the animationImages property contains a value other than nil, the contents of this property are not used."

UIImageView类上的image属性具有以下文档:“如果animationImages属性包含的值不是nil,则不使用此属性的内容。”

So the trick to hold on the last frame of an animation in iOS4+ is to first set the image property to that last frame (while animationImages is still nil), then set the animationImages property and call startAnimating. When the animation completes, the image property is then displayed. No callback/delegate needed.

因此,在iOS4 +中保持动画的最后一帧的技巧是首先将图像属性设置为最后一帧(而animationImages仍为零),然后设置animationImages属性并调用startAnimating。动画完成后,将显示图像属性。不需要回调/委托。

#1


3  

Try this:

self.imgBaloon.animationRepeatCount = 1;

EDIT: As @Elliott Perry stated in his answer, you need to assign the last frame to the image property. Here is the code:

编辑:正如@Elliott Perry在他的回答中所说,你需要将最后一帧分配给image属性。这是代码:

self.imgBalloon.animationImages = balloonAnimationImages;
self.imgBalloon.animationDuration = 5.0f;
self.imgBaloon.animationRepeatCount = 1;
self.imgBaloon.image = [balloonAnimationImages lastObject];
[self.imgBalloon startAnimating]; 

#2


3  

From Can I stop UIImageView Animation at last frame?

从我可以在最后一帧停止UIImageView动画吗?

The image property on the UIImageView class has the following docs: "If the animationImages property contains a value other than nil, the contents of this property are not used."

UIImageView类上的image属性具有以下文档:“如果animationImages属性包含的值不是nil,则不使用此属性的内容。”

So the trick to hold on the last frame of an animation in iOS4+ is to first set the image property to that last frame (while animationImages is still nil), then set the animationImages property and call startAnimating. When the animation completes, the image property is then displayed. No callback/delegate needed.

因此,在iOS4 +中保持动画的最后一帧的技巧是首先将图像属性设置为最后一帧(而animationImages仍为零),然后设置animationImages属性并调用startAnimating。动画完成后,将显示图像属性。不需要回调/委托。