前言

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

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

运用说明

经过网络链接下载播映,也支撑Swift并发,现在暂时仅支撑只要一个json文件的Lottie的下面,后续会支撑zip压缩包的方法解压文件夹播映。


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资源的文件夹)


///在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文件


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或许图片名皆可。


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