OC调用 3.5.0 Swift版本 Lottie
// LottieView.swift
// Lottie-test
//
// Created by Macmini on 2022/10/26.
//
因为Lottie 没有暴露方法属性给OC文件 所以需要我们自建Swift文件 集成Lottie供OC调用
importUIKit
importLottie
// @objcMembers 暴露 所有方法 属性给OC文件
@objcMembers class LottieView: UIView {
//加载 URL 地址
func showAnimation(url:URL,completionBlock: LottieCompletionBlock? = nil) -> Void {
let animation = LottieAnimationView.init(frame: self.bounds)
animation.loopMode= .playOnce
animation.contentMode = .scaleAspectFit
self.addSubview(animation)
LottieAnimation.loadedFrom(url: url , closure: { ani in
animation.animation= ani
animation.play(completion: { completionin
ifThread.isMainThread{
completionBlock!(completion)
}else{
DispatchQueue.main.async{
completionBlock!(completion)
}
}
self.removeFromSuperview()
})
},animationCache: LRUAnimationCache.sharedCache)
}
// 加载 本地JSON文件
func show(name: String, completionBlock: LottieCompletionBlock? = nil) -> Void{
let lota = LottieAnimationView.init(frame: self.bounds)
lota.loopMode= .playOnce
lota.contentMode = .scaleAspectFit
self.addSubview(lota)
let animation =LottieAnimation.named(name,animationCache: LRUAnimationCache.sharedCache)
lota.animation= animation
lota.play{ completionin
if Thread.isMainThread {
completionBlock!(completion)
}else{
DispatchQueue.main.async{
completionBlock!(completion)
}
}
self.removeFromSuperview()
}
}
// 加载文件路径
func show(filePath:String, completionBlock: LottieCompletionBlock? = nil) -> Void {
let lota = LottieAnimationView.init(frame: self.bounds)
lota.loopMode= .playOnce
lota.contentMode = .scaleAspectFit
self.addSubview(lota)
let animation =LottieAnimation.filepath(filePath,animationCache: LRUAnimationCache.sharedCache)
lota.animation= animation
lota.play{ completionin
if Thread.isMainThread {
completionBlock!(completion)
}else{
DispatchQueue.main.async{
completionBlock!(completion)
}
}
self.removeFromSuperview()
}
}
}
以上为自建Swift文件内容
OC调用
LottieView * one = [[LottieView alloc]initWithFrame:CGRectMake(0,0,375,668)];
[UIApplication.sharedApplication.keyWindow addSubview:one];
// @"https://s3-lover-live.s3.ap-southeast-1.amazonaws.com/829e456bb72b8d09c7d1784d0edfe92f"
if(sender.tag==1) {
[one showAnimationWithUrl:[NSURL URLWithString:@"https://s3-lover-live.s3.ap-southeast-1.amazonaws.com/1c6592104bd4882793e54a9e53560f53"] completionBlock:^(BOOL completion) {
}];
}else //if (sender.tag == 2)
{
[one showWithName:@"data"completionBlock:^(BOOLcompletion) {
}];
}