关于使用YYAnimatedImageView 加载动画,可参考 博客 YYImage: 修复了iOS14的bug 本文主要介绍,无法重复播放的问题。
先上效果:
原因分析:
YYAnimatedImage 协议提供一个方法(animatedImageLoopCount)获取循环模式 0 循环播放,默认1 播放一次,YYImage,YYFrameImage等都遵循该协议,但该方法只读,因此只要设置该属性为0,即可循环播放
方案一:
监听 YYAnimatedImageView 的 currentAnimatedImageIndex 当 播放到总末尾(获取当前gif 总帧数可调用YYImage 的 animatedImageFrameCount 方法或者YYAnimatedImageView 的 私有属性 _totalFrameCount,使用 KVO 获取),调用 stopAnimating 方法重新播放
方法二:(推荐)
使用KVO直接设置私有属性为循环播放,即:
let _img = YYImage.init(named: "IMG_1375.GIF")
//let _count = _img?.animatedImageFrameCount()
//_img?.animatedImageLoopCount()
let _imgView = YYAnimatedImageView.init(image: _img)
_imgView.backgroundColor = .clear
_imgView.setValue(0, forKey: "_totalLoop")
有相关问题欢迎留言