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压缩包方式下载播放,后续后继续维护支持,有什么问题讨论也可以随时私信我。

相关推荐
Watermelo6172 分钟前
【前端实战】从 try-catch 回调到链式调用:一种更优雅的 async/await 错误处理方案
前端·javascript·网络·vue.js·算法·vue·用户体验
ShawnRacine2 分钟前
iOS开发-安装cocoapods
ios·xcode·cocoapods
木易 士心2 分钟前
NestJS 核心揭秘:InstanceWrapper 的艺术与前端缓存新思路
前端·缓存
00后程序员张3 分钟前
iOS 抓包工具实战指南,从代理到数据流,全流程工具分工解析
android·ios·小程序·https·uni-app·iphone·webview
IT_陈寒6 分钟前
SpringBoot 3.x性能优化实战:这5个配置让你的应用启动速度提升50%
前端·人工智能·后端
奶昔不会射手7 分钟前
css之如何获取祖先元素的宽高
前端·css
serve the people7 分钟前
滑块验证完整实现教程(前端 + 后端 + Nginx 集成)
运维·前端·nginx
yivifu8 分钟前
Excel中Lookup函数实现临界点归入下一个等级的方法
java·前端·excel
Wiktok10 分钟前
Tailwind CSS 自适应相关
前端·css·tailwindcss
LYFlied11 分钟前
【一句话概括】Vue2 和 Vue3 的 diff 算法区别
前端·vue.js·算法·diff