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

相关推荐
森林的尽头是阳光12 分钟前
vue防抖节流,全局定义,使用
前端·javascript·vue.js
YiHanXii14 分钟前
React.memo 小练习题 + 参考答案
前端·javascript·react.js
zero13_小葵司21 分钟前
Vue 3 前端工程化规范
前端·javascript·vue.js
Yolanda_202222 分钟前
vue-sync修饰符解析以及切换iframe页面进行保存提示功能的思路
前端·javascript·vue.js
伍哥的传说24 分钟前
Vite Plugin PWA – 零配置构建现代渐进式Web应用
开发语言·前端·javascript·web app·pwa·service worker·workbox
00后程序员张1 小时前
iOS App 混淆与加固对比 源码混淆与ipa文件混淆的区别、iOS代码保护与应用安全场景最佳实践
android·安全·ios·小程序·uni-app·iphone·webview
小桥风满袖1 小时前
极简三分钟ES6 - ES8中字符串扩展
前端·javascript
张拭心1 小时前
这就是流量的力量吗?用豆包 AI 编程做的xhs小组件帖子爆了
前端·ai编程·豆包marscode
少年阿闯~~1 小时前
CSS3的新特性
前端·javascript·css3