推荐一个动画框架Lottie

  Lottie 是个非常出名的动画框架,官网:https://airbnb.design/lottie/
UI设计者,做好动画效果后,导出json文件,以及相关的资源文件。用这个框架已经很久了, 之所以现在推荐主要是之前觉得框架很出名,没必要推荐了。由于我的项目最低兼容iOS8,所以一直是OC版本的,最近导入了一个动画效果,发现渐变色效果显示不出来,仔细看了版本记录,发现最新的版本最低兼容iOS9,而且是swift版的,我这边项目是OC的,为了能够使用写了swift类,包装了一下, 方便使用,没什么技术含量,就是不想重复劳动,我把代码封装了一下。源码内容:

import Foundation
import Lottie

@objcMembers public class JKLottieManager: NSObject {
   public class func initAnimationView(_ filePath:String,_ frame:CGRect) -> AnimationView{
        let animationView:AnimationView = AnimationView.init(filePath: filePath)
        animationView.frame = frame
        return animationView
    }
    
   public class func animationViewNamed(_ name:String, _ frame:CGRect) -> AnimationView{
        let animationView:AnimationView = AnimationView.init(name: name)
        animationView.frame = frame
        return animationView
    }
    
   public class func configLoopModel(_ loopModel:NSInteger, _ animationView:AnimationView ,_ count:CGFloat) -> Void {
        if loopModel == 0 {
            animationView.loopMode = .playOnce
        }else if loopModel == 1 {
            animationView.loopMode = .loop
        }else if loopModel == 2 {
            animationView.loopMode = .autoReverse
        }else if loopModel == 3 {
            animationView.loopMode = .repeat(Float(count))
        }else if loopModel == 4 {
            animationView.loopMode = .repeatBackwards(Float(count))
        }
        
    }
    
   public class func play(_ animationView:AnimationView,_ complete:((_ finished:Bool)->Void)?)->Void{
        animationView.play { (_ finished:Bool) in
            if let complete = complete{
                complete(finished)
            }
        }
        
    }
    
   public class func play(_ animationView:AnimationView, _ fromProgress:CGFloat, _ toProgress:CGFloat, _ complete:((_ finished:Bool)->Void)?) ->Void{
        animationView.play(fromProgress: fromProgress, toProgress: toProgress, loopMode: animationView.loopMode) { (_ finished:Bool) in
            if let complete = complete{
                complete(finished)
            }
        }
    }
    
   public class func play(_ animationView:AnimationView) ->Void{
        animationView.play()
    }
    
   public class func pause(_ animationView:AnimationView) ->Void{
        animationView.pause()
    }
    
   public class func stop(_ animationView:AnimationView) ->Void{
        animationView.stop()
    }
    
   public class func changeRate(_ animationView:AnimationView, _ rate:CGFloat) ->Void{
        animationView.animationSpeed = rate
    }
}

源码编译为OC内容如下:

@class AnimationView;

SWIFT_CLASS("_TtC16JKLottieMananger15JKLottieManager")
@interface JKLottieManager : NSObject
+ (AnimationView * _Nonnull)initAnimationView:(NSString * _Nonnull)filePath :(CGRect)frame SWIFT_METHOD_FAMILY(none) SWIFT_WARN_UNUSED_RESULT;
+ (AnimationView * _Nonnull)animationViewNamed:(NSString * _Nonnull)name :(CGRect)frame SWIFT_WARN_UNUSED_RESULT;
+ (void)configLoopModel:(NSInteger)loopModel :(AnimationView * _Nonnull)animationView :(CGFloat)count;
+ (void)play:(AnimationView * _Nonnull)animationView :(void (^ _Nullable)(BOOL))complete;
+ (void)play:(AnimationView * _Nonnull)animationView :(CGFloat)fromProgress :(CGFloat)toProgress :(void (^ _Nullable)(BOOL))complete;
+ (void)play:(AnimationView * _Nonnull)animationView;
+ (void)pause:(AnimationView * _Nonnull)animationView;
+ (void)stop:(AnimationView * _Nonnull)animationView;
+ (void)changeRate:(AnimationView * _Nonnull)animationView :(CGFloat)rate;
- (nonnull instancetype)init OBJC_DESIGNATED_INITIALIZER;
@end

源码地址:https://github.com/xindizhiyin2014/JKLottieMananger
可以用pod集成 pod 'JKLottieMananger'

更多优质文章,可以微信扫码关注:
这里写图片描述

发布了231 篇原创文章 · 获赞 110 · 访问量 60万+

猜你喜欢

转载自blog.csdn.net/HHL110120/article/details/94765434