iOS下支持动态替换资源的Lottie库-JFDynamicLottie开源啦~

前言

在我iOS简单优化Lottie-OC版源码内存占用降低50%以上以及记一次开源框架lottie-ios的Pull Request并成功被合并的全过程两篇文章中,提到近期我在整理项目的Lottie从OC版本,切换到Swift版本。旧的OC版其实也是自己实现了一套动态替换的Lottie扩展的,但是切换到Swift版后基于OC版定制的方法以依然失效了,加之基于性能的考虑,近期也是成功的封装了内部支持动态替换资源以及优化性能后的Lottie库,顺便借此机会我也开源出来,给大伙做下参考参考啦~如果不介意的话,直接使用也是可以的,优化后的JFDynamicLottie不仅性能对于官方原版有一定提升,它还集成了动态替换图片、文本等资源的方法非常方便。下面是JFDynamicLottie这个库的简单使用说明。

使用说明

通过网络链接下载播放,也支持Swift并发,目前暂时仅支持只有一个json文件的Lottie的下面,后续会支持zip压缩包的方式解压文件夹播放。

swift 复制代码
if #available(iOS 13.0, *) {

Task {

guard let view = await JFLottieAnimationView.network(fromJson: URL(string: "http://image.jerryfans.com/lottie_data.json")!) else { return }

self.lottieView?.stop()

self.lottieView?.removeFromSuperview()

self.bgView.addSubview(view)

let size = self.bgView.frame.size

view.frame = CGRect(x: 0, y: 0, width: size.width, height: size.height)

view.loopMode = .loop

view.play()

self.lottieView = view

}

} else {

JFLottieAnimationView.network(fromJson: URL(string: "http://image.jerryfans.com/lottie_data.json")!) { [weak self] animationView in

guard let self = self else { return }

self.lottieView?.stop()

self.lottieView?.removeFromSuperview()

guard let view = animationView else { return }

self.bgView.addSubview(view)

let size = self.bgView.frame.size

view.frame = CGRect(x: 0, y: 0, width: size.width, height: size.height)

view.loopMode = .loop

view.play()

self.lottieView = view

}

}

通过本地mainBundle方式播放,需要先设置一个主要Bundle文件夹(本地存放Lottie资源的文件夹)

arduino 复制代码
///在AppDidFinish时候设置主目录,这样后续就可以直接听过mainBundle内部Lottie文件播放

JFLottieAnimationView.setupMainBundleDirectoryPath(path: "Resource/")

let view = JFLottieAnimationView.mainBundle(directoryPath: "slog_zm_effect_1")

self.bgView.addSubview(view)

let size = self.bgView.frame.size

view.frame = CGRect(x: 0, y: 0, width: size.width, height: size.height)

view.loopMode = .loop

view.play()

self.lottieView = view

通过绝对路径方式播放本地Lottie文件

ini 复制代码
guard let path = Bundle.main.path(forResource: "data", ofType: "json") else { return }

let view = JFLottieAnimationView.file(filePath: path)

self.bgView.addSubview(view)

let size = self.bgView.frame.size

view.frame = CGRect(x: 0, y: 0, width: size.width, height: size.height)

view.loopMode = .loop

view.play()

self.lottieView = view

动态替换方式,创建JFLottieAnimationView后,自己查看Lottie的json文件,动态替换相应的key,textReplacement替换文字id,imageReplacement替换图片id或者图片名皆可。

ini 复制代码
var textReplacement: [String:String] = [:]

textReplacement["我是用户名1"] = "JerryFans"

textReplacement["我是用户名2"] = "我是被替换的"

textReplacement["我是用户名5"] = "替换后的名字"

var imgReplacement: [String:UIImage] = [:]

let imgView = UIImageView(image: UIImage(named: "snap"))

imgView.frame = CGRect(x: 0, y: 0, width: 92, height: 92)

imgView.layer.cornerRadius = 46

imgView.layer.masksToBounds = true

if let img = imgView.jf.syncSnapshotImage() {

imgReplacement["head_0"] = img

}

self.lottieView?.imageReplacement = imgReplacement

self.lottieView?.textReplacement = textReplacement

下载地址

下面是Github地址,也支持CocoaPods的方式导入

JFDynamicLottie

结尾

以上就是JFDynamicLottie基于Lottie库封装的支持动态替换lottie资源的Lottie组件,由于时间的关系,暂时还不支持网络zip压缩包方式下载播放,后续后继续维护支持,有什么问题讨论也可以随时私信我。

相关推荐
2601_955767425 分钟前
圆偏振光AR膜实测:反射率≤0.5%+96%透光率,iPhone17 Pro贴膜久看不累——观复盾上手
人工智能·科技·ios·ar·iphone·圆偏振光
前端不太难7 分钟前
Edge AI 时代:从数据中心到终端,算力如何无处不在?
前端·人工智能·edge
Highcharts.js8 分钟前
Highcharts v13 全新时间轴标签边界格式|让时间维度表达更智能
前端·信息可视化·时间序列·图表开发·chart·自定义标签·可视化开发
lichenyang4539 分钟前
鸿蒙研读 10:@Provider/@Consumer、RelativeContainer、onNewWant
前端
大湿兄啊啊啊14 分钟前
MID360S调试
java·服务器·前端
绺年16 分钟前
模块化加载机制与循环依赖的探索
前端
Csvn25 分钟前
前端技术 - 前端技术债务
前端
Days205035 分钟前
Flyfish Viewer:全能纯前端多格式文件预览组件,解锁浏览器端无门槛预览新体验
前端
selfsuer36 分钟前
【奇奇怪怪前端问题记录】
前端
不要额外加糖38 分钟前
给 Codex 戴上紧箍, 治一治 AI 的过度发挥
前端·人工智能·代码规范