采用 FLAnimatedImage 加载gif
采用lottie-ios 加载json 动画
//// NSString *gifPath = [[GlobalBundle classBundle] pathForResource:@"shopbagbottom" ofType:@"gif"];
//// NSData *gifData = [NSData dataWithContentsOfFile:gifPath];
//// FLAnimatedImage *gif = [FLAnimatedImage animatedImageWithGIFData:gifData];
//// _iconView.animatedImage = gif;
// //先使用这种方式加载动画,导出的gif锯齿明显,json需要导入lot库,目前有些lot是oc有些是swift,后面替换json即可
//// NSMutableArray *imageArray = @[].mutableCopy;
//// for (NSInteger i=0; i<68; i++) {
//// UIImage *image = [GlobalBundle imageForName:[NSString stringWithFormat:@"合成 1_000%02ld",(long)i]];
//// if (image) {
//// [imageArray addObject:image];
//// }
//// }
//// UIImage *image = [GlobalBundle imageForNameForGif:@"shopbagbottom.gif"];
//// _iconView.image = image;
//// [imageArray addObject:image];
//// _iconView.animationImages = imageArray;
//// _iconView.animationDuration = 2.83;
//
//// _iconView.animationRepeatCount = 1;
// _iconView.hidden = YES;
// [_iconView stopAnimating];
// [self addSubview:_iconView];
//
// [_iconView mas_remakeConstraints:^(MASConstraintMaker *make) {
//// make.bottom.equalTo(self).offset(-1);
// make.centerY.mas_equalTo(-25);
// make.centerX.mas_equalTo(-6);
// make.width.mas_equalTo(100);
// make.height.mas_equalTo(65);
// }];
ob 使用需要需要桥接一下,使用方便
import Foundation
import UIKit
import Lottie
class HQEduAnimationView : UIView
{
var animationView:AnimationView?
public var animationName:String? {
didSet {
let animation = Animation.named(animationName ?? "", bundle: Bundle.main, subdirectory: nil, animationCache: nil)
animationView?.animation = animation
}
}
override init(frame: CGRect) {
super.init(frame: frame)
animationView = AnimationView.init()
animationView!.contentMode = .scaleAspectFit
animationView!.loopMode = .playOnce
animationView!.bounds = frame
animationView!.backgroundBehavior = .pauseAndRestore;
self.addSubview(animationView!)
animationView?.mas_makeConstraints({ (make) in
// make?.left.mas_equalTo()(0)
// make?.top.mas_equalTo()(0)
// make?.right.mas_equalTo()(0)
// make?.bottom.mas_equalTo()(0)
make?.width.height().mas_equalTo()(self)
make?.centerX.centerY().mas_equalTo()(0)
})
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
@objc public convenience init(contentsOfURL:URL,
loop:Bool,
completion: LottieCompletionBlock? = nil) {
self.init()
self.updateLoopAnimation(loop: loop)
Animation.loadedFrom(url: contentsOfURL, closure: { [weak self](anim) in
guard let `self` = self else { return }
self.animationView?.animation = anim
self.animationView?.play(completion: completion)
}, animationCache: nil)
}
@objc public func play(animationName:String,
completion: LottieCompletionBlock? = nil) {
let animation = Animation.named(animationName, bundle: GlobalBundle.classBundle(), subdirectory: nil, animationCache: nil)
self.animationView?.animation = animation
self.animationView?.play(completion: completion)
}
@objc public func appStartAnimationPlay(animationName:String, offsetY:CGFloat,
completion: LottieCompletionBlock? = nil) {
let animation = Animation.named(animationName, bundle: GlobalBundle.classBundle(), subdirectory: nil, animationCache: nil)
self.animationView?.animation = animation
let isIPad = UIDevice.current.userInterfaceIdiom == .pad
if isIPad == false {
self.animationView?.mas_updateConstraints({ (make) in
make?.centerY.mas_equalTo()(offsetY)
})
}
self.animationView?.play(completion: completion)
}
@objc public func play(filePath:String,
completion: LottieCompletionBlock? = nil) {
let animation = Animation.filepath(filePath, animationCache: LRUAnimationCache.sharedCache)
self.animationView?.animation = animation
self.animationView?.play(completion: completion)
}
@objc public func play(completion: LottieCompletionBlock? = nil) {
self.animationView?.play(completion: completion)
}
@objc class func animation(filePath: String) -> HQEduAnimationView {
let animation = Animation.filepath(filePath, animationCache: nil)
let animationView = HQEduAnimationView.init()
if (animation != nil) {
animationView.addAnimation(animation: animation!)
}
return animationView;
}
func addAnimation(animation: Animation) {
self.animationView?.animation = animation
}
@objc public func play(animationName:String,
loop:Bool,
completion: LottieCompletionBlock? = nil) {
let animation = Animation.named(animationName, bundle: GlobalBundle.classBundle(), subdirectory: nil, animationCache: nil)
self.animationView?.animation = animation
if loop {
self.animationView?.loopMode = .loop
} else {
self.animationView?.loopMode = .playOnce
}
self.animationView?.play(completion: completion)
}
@objc public func pause() {
self.animationView?.pause()
}
@objc public func play() {
self.animationView?.play()
}
@objc public func stop() {
self.animationView?.stop()
}
@objc public func isPlaying() -> Bool {
return ((self.animationView?.isAnimationPlaying) != nil)
}
@objc public func setCurrentProgress(progress:CGFloat) {
self.animationView?.currentProgress = progress
}
@objc public func setAnimationSpeed(speed:CGFloat) {
self.animationView?.animationSpeed = speed
}
@objc public func getAnimationNamed() -> String{
return self.animationName ?? ""
}
@objc public func play(fromProgress:CGFloat,
toProgress:CGFloat,
completion: LottieCompletionBlock? = nil) {
self.animationView?.play(fromProgress: fromProgress, toProgress: toProgress,completion: completion)
}
@objc public func updateLoopAnimation(loop:Bool) {
if loop {
self.animationView?.loopMode = .loop
} else {
self.animationView?.loopMode = .playOnce
}
}
}