2025-08-08 10:49:36 +08:00
|
|
|
//
|
|
|
|
|
// QXEffectSvgaView.m
|
|
|
|
|
// SoundRiver
|
|
|
|
|
//
|
|
|
|
|
// Created by 段智博 on 2020/10/26.
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
#import "QXEffectSvgaView.h"
|
|
|
|
|
#import <SVGA.h>
|
|
|
|
|
|
|
|
|
|
@interface QXEffectSvgaView ()<SVGAPlayerDelegate>
|
|
|
|
|
//播放动画
|
|
|
|
|
@property (nonatomic,strong) SVGAPlayer *player;
|
|
|
|
|
@property (nonatomic,strong) SVGAParser *parser;
|
|
|
|
|
|
|
|
|
|
@property (nonatomic,assign) BOOL isAutoPlay;
|
|
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
|
|
@implementation QXEffectSvgaView
|
|
|
|
|
|
|
|
|
|
- (instancetype)initWithFrame:(CGRect)frame isAutoPlay:(BOOL)isAutoPlay {
|
|
|
|
|
self = [super initWithFrame:frame];
|
|
|
|
|
if (self) {
|
|
|
|
|
[self initalizeData];
|
|
|
|
|
self.isAutoPlay = isAutoPlay;
|
|
|
|
|
}
|
|
|
|
|
return self;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (instancetype)initWithFrame:(CGRect)frame {
|
|
|
|
|
self = [super initWithFrame:frame];
|
|
|
|
|
if (self) {
|
|
|
|
|
[self initalizeData];
|
|
|
|
|
}
|
|
|
|
|
return self;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void)initalizeData {
|
|
|
|
|
self.backgroundColor = [UIColor clearColor];
|
|
|
|
|
self.userInteractionEnabled = NO;
|
|
|
|
|
self.isAutoPlay = YES;
|
|
|
|
|
[self initPlayer];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void)initPlayer {
|
|
|
|
|
[self addSubview:self.player];
|
|
|
|
|
[self.player mas_makeConstraints:^(MASConstraintMaker *make) {
|
|
|
|
|
make.edges.equalTo(self);
|
|
|
|
|
}];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void)loadSVGAPlayerWith:(NSString *)loadPath {
|
|
|
|
|
[self loadSVGAPlayerWith:loadPath inBundle:NO];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void)loadSVGAPlayerWith:(NSString *)loadPath inBundle:(BOOL)inBundle {
|
|
|
|
|
[self loadSVGAPlayerWith:loadPath inBundle:inBundle loop:1];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void)loadSVGAPlayerWith:(NSString *)loadPath inBundle:(BOOL)inBundle loop:(int)loop {
|
|
|
|
|
if (![loadPath isExist]) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (!_player) {
|
|
|
|
|
[self initPlayer];
|
|
|
|
|
}
|
|
|
|
|
[self stopEffectSvgaPlay];
|
|
|
|
|
|
|
|
|
|
self.player.loops = loop;
|
|
|
|
|
|
|
|
|
|
__weak typeof(self)weakSelf = self;
|
|
|
|
|
if ([loadPath hasPrefix:@"https:"] || [loadPath hasPrefix:@"http:"]) {
|
|
|
|
|
/// URL
|
|
|
|
|
[self.parser parseWithURL:[NSURL URLWithString:loadPath] completionBlock:^(SVGAVideoEntity * _Nullable videoItem) {
|
|
|
|
|
if (videoItem != nil) {
|
|
|
|
|
weakSelf.player.videoItem = videoItem;
|
|
|
|
|
if (weakSelf.isAutoPlay) {
|
|
|
|
|
[weakSelf.player startAnimation];
|
|
|
|
|
if (weakSelf.didStartAnimation) {
|
|
|
|
|
weakSelf.didStartAnimation();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} failureBlock:^(NSError * _Nullable error) {
|
|
|
|
|
if (weakSelf.didFinishedDisplay) {
|
|
|
|
|
weakSelf.didFinishedDisplay(weakSelf);
|
|
|
|
|
}
|
|
|
|
|
}];
|
|
|
|
|
}else if (inBundle) {
|
|
|
|
|
[self.parser parseWithNamed:loadPath inBundle:[NSBundle mainBundle] completionBlock:^(SVGAVideoEntity * _Nonnull videoItem) {
|
|
|
|
|
if (videoItem != nil) {
|
|
|
|
|
weakSelf.player.videoItem = videoItem;
|
|
|
|
|
if (weakSelf.isAutoPlay) {
|
|
|
|
|
[weakSelf.player startAnimation];
|
|
|
|
|
if (weakSelf.didStartAnimation) {
|
|
|
|
|
weakSelf.didStartAnimation();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} failureBlock:^(NSError * _Nonnull error) {
|
|
|
|
|
if (weakSelf.didFinishedDisplay) {
|
|
|
|
|
weakSelf.didFinishedDisplay(weakSelf);
|
|
|
|
|
}
|
|
|
|
|
}];
|
|
|
|
|
} else {
|
|
|
|
|
NSData *data = [NSData dataWithContentsOfFile:loadPath];
|
|
|
|
|
if (!data || data.length < 4) {
|
|
|
|
|
if (weakSelf.didFinishedDisplay) {
|
|
|
|
|
weakSelf.didFinishedDisplay(weakSelf);
|
|
|
|
|
}
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
[self.parser parseWithData:data cacheKey:loadPath completionBlock:^(SVGAVideoEntity * _Nonnull videoItem) {
|
|
|
|
|
if (videoItem != nil) {
|
|
|
|
|
weakSelf.player.videoItem = videoItem;
|
|
|
|
|
if (weakSelf.isAutoPlay) {
|
|
|
|
|
[weakSelf.player startAnimation];
|
|
|
|
|
if (weakSelf.didStartAnimation) {
|
|
|
|
|
weakSelf.didStartAnimation();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} failureBlock:^(NSError * _Nonnull error) {
|
|
|
|
|
if (weakSelf.didFinishedDisplay) {
|
|
|
|
|
weakSelf.didFinishedDisplay(weakSelf);
|
|
|
|
|
}
|
|
|
|
|
}];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#pragma mark - Public
|
|
|
|
|
- (void)startEffectSvgaPlay {
|
|
|
|
|
if (self.player.videoItem) {
|
|
|
|
|
[self.player startAnimation];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
- (void)pauseEffectSvgaPlay {
|
|
|
|
|
[self.player pauseAnimation];
|
|
|
|
|
}
|
|
|
|
|
- (void)stopEffectSvgaPlay {
|
|
|
|
|
[self.player stopAnimation];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void)destroySvga {
|
|
|
|
|
[self.player stopAnimation];
|
|
|
|
|
[self.player removeFromSuperview];
|
|
|
|
|
_player = nil;
|
|
|
|
|
_parser = nil;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#pragma mark - SVGAPlayerDelegate
|
|
|
|
|
- (void)svgaPlayerDidFinishedAnimation:(SVGAPlayer *)player {
|
2025-10-20 09:43:10 +08:00
|
|
|
[self.player stopAnimation];
|
2025-08-08 10:49:36 +08:00
|
|
|
if (self.didFinishedDisplay) {
|
|
|
|
|
self.didFinishedDisplay(self);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#pragma mark - Getter
|
|
|
|
|
// 动画
|
|
|
|
|
- (SVGAPlayer *)player {
|
|
|
|
|
if (!_player) {
|
|
|
|
|
_player = [[SVGAPlayer alloc] initWithFrame:CGRectZero];
|
|
|
|
|
_player.backgroundColor = [UIColor clearColor];
|
|
|
|
|
_player.contentMode = UIViewContentModeScaleAspectFill;
|
|
|
|
|
// _player.contentMode = UIViewContentModeScaleAspectFit;
|
|
|
|
|
_player.clipsToBounds = YES;
|
|
|
|
|
_player.delegate = self;
|
|
|
|
|
_player.clearsAfterStop = YES;
|
|
|
|
|
}
|
|
|
|
|
return _player;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (SVGAParser *)parser {
|
|
|
|
|
if (!_parser) {
|
|
|
|
|
_parser = [[SVGAParser alloc] init];
|
|
|
|
|
}
|
|
|
|
|
return _parser;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@end
|