我使用了由"mayoff“(Rob Mayoff) "UIImageView+animatedGIF”制作的类,这是在stackoverflow上的一个答案中提出的。UIImageView+animatedGIF
有了它,我可以在iOS上的UIImageView中导入.gif动画图像。这个类工作得无懈可击,但唯一的问题是.gif总是循环。无论我如何导出它(我从photoshop中导出图像- 67帧,将重复设置为“一次”),它在UIImageView中永远循环。
我用这两行代码导入我的.gif:
NSURL *url = [[NSBundle mainBundle] URLForResource:@"Loading" withExtension:@"gif"];
self.loadingImageView.image = [UIImage animatedImageWithAnimatedGIFData:[NSData dataWithContentsOfURL:url]];非常简单,而且很管用。我试着将animationRepeatCount属性设置为1,这是Rob推荐的,但没有起到作用。此外,我还尝试将UIImageView的animationImages属性设置为gifImage.images,而不仅仅是将视图的image属性设置为gifImage。在这种情况下,.gif根本不是动画。
有什么办法只玩一次.gif吗?我可以想到许多技巧(比如将gif的最后一帧设置为在一段时间后显示,但如果可能的话,我会先尝试更简单的方法)。
发布于 2013-07-28 12:52:09
我从该项目中获取了测试应用程序,并将viewDidLoad方法更改为:
- (void)viewDidLoad {
[super viewDidLoad];
NSURL *url = [[NSBundle mainBundle] URLForResource:@"test" withExtension:@"gif"];
UIImage *testImage = [UIImage animatedImageWithAnimatedGIFData:[NSData dataWithContentsOfURL:url]];
self.dataImageView.animationImages = testImage.images;
self.dataImageView.animationDuration = testImage.duration;
self.dataImageView.animationRepeatCount = 1;
self.dataImageView.image = testImage.images.lastObject;
[self.dataImageView startAnimating];
}当应用程序启动时,它会对图像进行一次动画处理,然后只显示最后一帧。如果我连接了一个发送[self.dataImageView startAnimating]的按钮,则轻触该按钮将再次运行动画,然后在最后一帧再次停止。
确保你按照我上面的方式设置你的图像视图。确保您正在而不是将图像视图的image属性设置为动画图像。如果要停止动画,则必须将图像视图的image属性设置为非动画图像。
发布于 2014-05-01 17:19:27
我已经派生了由"Dreddik“制作的"uiimage-from-animated-gif”,并添加了一个委托方法。
#pragma mark - AnimatedGifDelegate
- (void)animationWillRepeat:(AnimatedGif *)animatedGif
{
NSLog(@"animationWillRepeat");
//[animatedGif stop];
}所以你知道动画会在什么时候重复,并在每次重复的过程中做你自己的事情。您可以计算动画重复的次数,并在达到一定次数时停止动画。
发布于 2020-08-19 05:58:42
在启动/stp动画和管理动画计数时遇到问题,因此使用了this pod
imageViewObject.animate(withGIFNamed: "gif_name", loopCount: 1) {
print("animating image")
}要启动/停止动画:
imageViewObject.isAnimatingGIF ? imageViewObject.stopAnimatingGIF() : imageViewObject.startAnimatingGIF() https://stackoverflow.com/questions/17864292
复制相似问题