【问题标题】:Stop animating GIF after one revolution旋转一圈后停止动画 GIF
【发布时间】:2014-08-12 05:38:07
【问题描述】:

我正在使用uiimage-from-animated-gif 库通过UIImage 显示动画GIF。它工作正常,但我需要在转一圈后停止动画或停止显示imageView2

我使用以下代码为 GIF 制作动画:

NSURL *url2 = [[NSBundle mainBundle] URLForResource:@"dove-animate" withExtension:@"gif"];
self.imageView2.image = [UIImage animatedImageWithAnimatedGIFData:[NSData dataWithContentsOfURL:url2]];

【问题讨论】:

  • 你试过设置self.imageView2.animationRepeatCount吗?
  • imageview.animationrepeatcount 没有变化
  • 如果你可以将 gif 图像拆分为多个图像,并将它们放入 UIImageView 中,那么它会起作用,但不确定是否取决于你.. 否则不确定抱歉

标签: objective-c cocoa-touch uiimage


【解决方案1】:

如果您知道动画持续时间,那么您可以在该持续时间之后为您的图像视图设置一个静态图像。

例如,如果持续时间是 2 秒,那么

[self performSelector:@selector(stopAnimating) withObject:nil afterDelay:2.0f];

-(void)stopAnimating{
    [self.imageView2 setImage:[UIImage imageNamed:@"dove-animate.gif"]];
}

【讨论】:

  • 你忘了添加计算 GIF 动画持续时间的方法,这只是一个半解决方案,因为每个 GIF 动画都有不同的持续时间。
  • 我说如果你知道动画持续时间然后它的工作。否则他必须用计时器或委托编辑 UIImage+animatedGIF 类别静态 UIImage *animatedImageWithAnimatedGIFImageSource(CGImageSourceRef const source) 方法
  • 是的,确实如此,但它并没有真正的帮助,因为问题是如何计算动画的持续时间?。没有它,这只是一个半答案。作为一个理论,它可以是好的,但它在实践中是无用的。
【解决方案2】:

如果您将图像存储在包中而不是从网络加载它们,则无需求助于 gif。实际上,您可以拥有一个完整的图像文件夹(png 的质量很好并提供透明度)。那么:

  1. 通过UIImage + imageNamed: 或类似方法将每个图像加载到UIImage
  2. 创建UIImageView 并将animationImages 设置为您的数组。
  3. 根据需要在图像视图上调用startAnimatingstopAnimating

【讨论】:

    【解决方案3】:

    添加计时器以使图像视图消失。然后似乎动画停止了

    {
    NSTimer *timer;
    
        timer =  [NSTimer scheduledTimerWithTimeInterval: 4
                                                  target: self
                                                selector: @selector(removeAnimation)
                                                userInfo: nil
                                                 repeats: NO];
    }
    
    -(void)removeAnimation
    {
    imageview.hidden=yes;
    }
    

    【讨论】:

      猜你喜欢
      • 2013-02-07
      • 2018-11-17
      • 2014-12-02
      • 1970-01-01
      • 2014-04-23
      • 2021-07-21
      • 2015-03-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多