简书好文章搬运
iOS GIF动画加载框架-FLAnimatedImage解读
最近在做一个App启动动画,最终决定用加载GIF图片的方式实现,选择了FLAnimatedImage这个框架。加载GIF没问题,问题出在如何让GIF仅循环一次?
解决方案如下
Make loopCount writable so one can set the animation loop count #83
关键点就是用到了FLAnimatedImageView
中的
@property (nonatomic, copy) void(^loopCompletionBlock)(NSUInteger loopCountRemaining);
这个Block回调在每次GIF执行一次循环后都会调用,NSUInteger loopCountRemaining
这个参数在这个版本中没看到有什么用,开始值就是NSUInteger的最大值,所以想循环几次还是自己定义一个参数,++就可以了。
自己的代码
@property (nonatomic, strong) FLAnimatedImageView *gifImageView;
- (void)viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:animated];
[self.gifImageView removeFromSuperview];
}
NSURL *gifLocalUrl = [[NSBundle mainBundle] URLForResource:@"Splash" withExtension:@"gif"];
NSData *gifData = [NSData dataWithContentsOfURL:gifLocalUrl];
FLAnimatedImage *image = [FLAnimatedImage animatedImageWithGIFData:gifData];
self.gifImageView = [[FLAnimatedImageView alloc] init];
self.gifImageView.animatedImage = image;
self.gifImageView.frame = CGRectMake(0.0, kScreenHeight - kScreenWidth*(524/750.0) - kScreenWidth*(110/375.0f), kScreenWidth, kScreenWidth*(524/750.0));
__weak typeof (self) weakSelf = self;
//只播放一次 这个block每次执行一次循环就会走一次的self.gifImageView.loopCompletionBlock = ^(NSUInteger loopCount){
[weakSelf endSplashView];
};
[self.view addSubview:self.gifImageView];
}
- (void)endSplashView
{
if (self.finishBlock)
{
self.finishBlock();
}
}