前言
遇到个需求要先播放一个webp动画,然后停到最后一帧,YYAnimatedImageView刚好满足需求。
实现
YYAnimatedImageView *successanimatedImageView = [[YYAnimatedImageView alloc] initWithFrame:CGRectZero];
/* 监听是否播放到最后一帧,如果是将最后一个一帧取出来*/
[[RACObserve(successanimatedImageView,currentAnimatedImageIndex)
takeUntil:self.rac_willDeallocSignal]
subscribeNext:^(NSNumber *indeXCurrent) {
@strongify(successanimatedImageView);
if (indeXCurrent) {
NSNumber *totalFrameCount = [successanimatedImageView valueForKey:@"_totalFrameCount"];
if (indeXCurrent.integerValue == totalFrameCount.integerValue - 1) {
/* 需要注意的是_curFrame也能取到,但是currentAnimatedImageIndex变化之后才会赋值*/
image = [successanimatedImageView valueForKey:@"_curAnimatedImage"];
}
}
}];
/*from YYImage */
[self willChangeValueForKey:@"currentAnimatedImageIndex"];
_curIndex = currentAnimatedImageIndex;
[self didChangeValueForKey:@"currentAnimatedImageIndex"];
_curFrame = [_curAnimatedImage animatedImageFrameAtIndex:_curIndex];
写在最后
我们也可以利用 currentAnimatedImageIndex
和currentIsPlayingAnimation
去监听,在YYImage的issues55作者也推荐使用.
1.判断当前currentAnimatedImageIndex是否等于self.image.count -1,然后stopAnimation。
2.判断播放状态,(需要注意的是_totalLoop可能是0,0代码着无限,不停止的话,动画就会一直播放,别忘记第一步的stopAnimation.)
网友评论