pod 'SVGAPlayer', '~> 2.3'
pod 'YYKit'
SVGAPlayer播放动画,YYKit是为了用它的队列数组YYThreadSafeArray。
创建一个动画管理类:
SVGAAnimationManager.h
//
// SVGAAnimationManager.h
// Demo
//
// Created by 倪大头 on 2018/12/22.
// Copyright © 2018年 倪大头. All rights reserved.
//
#import <Foundation/Foundation.h>
@class SVGAAnimationManager;
@protocol SVGAAnimationManagerDelegate <NSObject>
@optional;
- (void)SVGAAnimationManager:(SVGAAnimationManager *)svgaAnimationManager showSVGAAnimation:(NSString *)animationUrl;
@end
@interface SVGAAnimationManager : NSObject
//动画队列
@property (nonatomic, strong) YYThreadSafeArray *svgaAnimationArray;
//动画是否正在播放
@property (nonatomic, assign) BOOL isShowSVGAAnimation;
@property (nonatomic, weak) id <SVGAAnimationManagerDelegate> delegate;
- (void)addSvgaAnimation;
- (void)destroy;
@end
SVGAAnimationManager.m
//
// SVGAAnimationManager.m
// Demo
//
// Created by 倪大头 on 2018/12/22.
// Copyright © 2018年 倪大头. All rights reserved.
//
#import "SVGAAnimationManager.h"
@implementation SVGAAnimationManager
- (instancetype)init {
self = [super init];
if (self) {
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(addQueueSVGAAnimation:) name:@"addAnimation" object:nil];//监听一个发送礼物的通知,来模拟socket消息
}
return self;
}
//接到发送礼物的消息,把动画url添加进队列数组
- (void)addQueueSVGAAnimation:(NSNotification *)noti {
if ([noti.object isKindOfClass:[NSString class]]) {
[self.svgaAnimationArray addObject:noti.object];
[self addSvgaAnimation];
}
}
//如果礼物动画正在播放,不打断
- (void)addSvgaAnimation {
if (!_isShowSVGAAnimation) {
if (self.svgaAnimationArray.count == 0) return;
if ([self.delegate respondsToSelector:@selector(SVGAAnimationManager:showSVGAAnimation:)]) {
[self.delegate SVGAAnimationManager:self showSVGAAnimation:self.svgaAnimationArray.firstObject];
[self.svgaAnimationArray removeFirstObject];
}
}
}
- (YYThreadSafeArray *)svgaAnimationArray {
if (!_svgaAnimationArray) {
_svgaAnimationArray = [YYThreadSafeArray array];
}
return _svgaAnimationArray;
}
- (void)destroy {
[self.svgaAnimationArray removeAllObjects];
self.delegate = nil;
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
@end
创建一个用于播放动画的VC:
SVGAPlayerVC.m
//
// SVGAPlayerVC.m
// Demo
//
// Created by 倪大头 on 2018/12/22.
// Copyright © 2018年 倪大头. All rights reserved.
//
#import "SVGAPlayerVC.h"
#import "SVGA.h"
#import "SVGAAnimationManager.h"
#import "UIButton+touch.h"
@interface SVGAPlayerVC() <SVGAPlayerDelegate, SVGAAnimationManagerDelegate>
@property (nonatomic, strong) SVGAPlayer *aPlayer;
@property (nonatomic, strong) SVGAAnimationManager *svgaAnimationManager;
@end
@implementation SVGAPlayerVC
static SVGAParser *parser;
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor blackColor];
[self createUI];
}
- (void)createUI {
//初始化svga动画管理器
_svgaAnimationManager = [[SVGAAnimationManager alloc]init];
_svgaAnimationManager.delegate = self;
//添加播放器
[self.view addSubview:self.aPlayer];
self.aPlayer.delegate = self;
self.aPlayer.frame = CGRectMake(0, 64, UI_SCREEN_WIDTH, UI_SCREEN_WIDTH);
self.aPlayer.loops = 1;
self.aPlayer.clearsAfterStop = YES;
parser = [[SVGAParser alloc] init];
UIButton *sendBtn = [[UIButton alloc]initWithFrame:CGRectMake(40, UI_SCREEN_HEIGHT - 64 - 100, UI_SCREEN_WIDTH - 80, 45)];
[sendBtn setTitle:@"send" forState:UIControlStateNormal];
[sendBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
sendBtn.titleLabel.font = [UIFont systemFontOfSize:15];
sendBtn.backgroundColor = [UIColor whiteColor];
sendBtn.isIgnore = YES;
[self.view addSubview:sendBtn];
[sendBtn addTarget:self action:@selector(addSVGAAnimation) forControlEvents:UIControlEventTouchUpInside];
}
- (void)viewWillLayoutSubviews {
[super viewWillLayoutSubviews];
self.aPlayer.frame = CGRectMake(0, 64, self.view.bounds.size.width, self.view.bounds.size.height);
}
//随机选一个动画,模拟服务器返回
- (void)addSVGAAnimation {
NSArray *items = @[
@"https://github.com/yyued/SVGA-Samples/blob/master/EmptyState.svga?raw=true",
@"https://github.com/yyued/SVGA-Samples/blob/master/HamburgerArrow.svga?raw=true",
@"https://github.com/yyued/SVGA-Samples/blob/master/PinJump.svga?raw=true",
@"https://github.com/yyued/SVGA-Samples/blob/master/TwitterHeart.svga?raw=true",
@"https://github.com/yyued/SVGA-Samples/blob/master/Walkthrough.svga?raw=true",
@"https://github.com/yyued/SVGA-Samples/blob/master/angel.svga?raw=true",
@"https://github.com/yyued/SVGA-Samples/blob/master/halloween.svga?raw=true",
@"https://github.com/yyued/SVGA-Samples/blob/master/kingset.svga?raw=true",
@"https://github.com/yyued/SVGA-Samples/blob/master/posche.svga?raw=true",
@"https://github.com/yyued/SVGA-Samples/blob/master/rose.svga?raw=true",
];
[[NSNotificationCenter defaultCenter] postNotificationName:@"addAnimation" object:items[arc4random() % 10]];
}
//播放动画
- (void)SVGAAnimationManager:(SVGAAnimationManager *)svgaAnimationManager showSVGAAnimation:(NSString *)animationUrl {
NSLog(@"动画URL %@", animationUrl);
_svgaAnimationManager.isShowSVGAAnimation = YES;
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
[parser parseWithURL:[NSURL URLWithString:animationUrl]
completionBlock:^(SVGAVideoEntity * _Nullable videoItem) {
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
if (videoItem != nil) {
self.aPlayer.videoItem = videoItem;
[self.aPlayer startAnimation];
}
} failureBlock:nil];
}
//播放完成改变正在播放的状态,并播放队列中下一个动画
- (void)svgaPlayerDidFinishedAnimation:(SVGAPlayer *)player {
NSLog(@"播放完成");
_svgaAnimationManager.isShowSVGAAnimation = NO;
[_svgaAnimationManager addSvgaAnimation];
}
- (SVGAPlayer *)aPlayer {
if (_aPlayer == nil) {
_aPlayer = [[SVGAPlayer alloc] init];
}
return _aPlayer;
}
@end