- 1. Create an Xcode-app project
- 2. Open -cd on the terminal and go to the project directory
- 3. Pod init Creates a Podfile
- 4. Write podfile
source 'https://mirrors.tuna.tsinghua.edu.cn/git/CocoaPods/Specs.git'
platform :ios, '9.0'
use_frameworks!
target 'SVGAPlayer - practice' do
pod 'SVGAPlayer'
end
Copy the code
- 5.pod install –no-repo-update
- 6.
#import <SVGAPlayer/SVGA.h>
Importing header files - 7.
@property (nonatomic, strong) SVGAPlayer *headFramePlayer; // Dynamic picture box
Member attribute - 8. Lazy loading
//https://qn-video.longyouquwa.cn/public/2021/4/kne9q64x1618212139664.svga
//https://appfile.longyouquwa.cn/upload/gift/48741577444818.svga
- (SVGAPlayer *)headFramePlayer{
if(! _headFramePlayer) { _headFramePlayer = [[SVGAPlayer alloc] initWithFrame:CGRectMake(0.70.100.100)];
_headFramePlayer.backgroundColor = [UIColor redColor];
_headFramePlayer.loops = 0;
_headFramePlayer.contentMode = UIViewContentModeScaleAspectFit;
_headFramePlayer.userInteractionEnabled = NO;
_headFramePlayer.clearsAfterStop = YES;
SVGAParser *parser = [[SVGAParser alloc] init];
__weak typeof(self) weakSelf = self;
[parser parseWithURL:[NSURL URLWithString:@"https://appfile.longyouquwa.cn/upload/gift/48741577444818.svga"] completionBlock:^(SVGAVideoEntity * _Nullable videoItem) {
if(videoItem ! =nil) {
weakSelf.headFramePlayer.videoItem = videoItem;
[weakSelf.headFramePlayer startAnimation];
}
} failureBlock:nil];
}
return _headFramePlayer;
}
Copy the code
- 9. Add it to the view
[self.view addSubview:self.headFramePlayer];
-
info.plist
addApp Transport Security Settings
–Allow Arbitrary Loads
Set to YES
- 11. Just run it
- 12. The source code
#import "ViewController.h"
#import <SVGAPlayer/SVGA.h>
@interface ViewController(a)
@property (nonatomic.strong) SVGAPlayer *headFramePlayer; // Dynamic picture box
@property (nonatomic.strong) SVGAPlayer *wavePlayer;
@property (nonatomic.strong) SVGAParser *waveParser;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self.view addSubview:self.headFramePlayer];
[self.view addSubview:self.wavePlayer];
}
//https://qn-video.longyouquwa.cn/public/2021/4/kne9q64x1618212139664.svga
//https://appfile.longyouquwa.cn/upload/gift/48741577444818.svga
- (SVGAPlayer *)headFramePlayer{
if(! _headFramePlayer) { _headFramePlayer = [[SVGAPlayer alloc] initWithFrame:CGRectMake(0.70.100.100)];
_headFramePlayer.backgroundColor = [UIColor redColor];
_headFramePlayer.loops = 0;
_headFramePlayer.contentMode = UIViewContentModeScaleAspectFit;
_headFramePlayer.userInteractionEnabled = NO;
_headFramePlayer.clearsAfterStop = YES;
SVGAParser *parser = [[SVGAParser alloc] init];
__weak typeof(self) weakSelf = self;
[parser parseWithURL:[NSURL URLWithString:@"https://appfile.longyouquwa.cn/upload/gift/48741577444818.svga"] completionBlock:^(SVGAVideoEntity * _Nullable videoItem) {
if(videoItem ! =nil) {
weakSelf.headFramePlayer.videoItem = videoItem;
[weakSelf.headFramePlayer startAnimation];
}
} failureBlock:nil];
}
return _headFramePlayer;
}
- (SVGAPlayer *)wavePlayer{
if(! _wavePlayer) { _wavePlayer = [[SVGAPlayer alloc] initWithFrame:CGRectMake(0.190.100.100)];
_wavePlayer.backgroundColor = [UIColor orangeColor];
_wavePlayer.loops = 0;
_wavePlayer.contentMode = UIViewContentModeScaleAspectFit;
_wavePlayer.userInteractionEnabled = NO;
_wavePlayer.clearsAfterStop = YES;
__weak typeof(self) weakSelf = self;
[self.waveParser parseWithNamed:@"sound_wave_left" inBundle:[NSBundle mainBundle] completionBlock:^(SVGAVideoEntity * _Nonnull videoItem) {
__strong typeof(weakSelf) stongSelf = weakSelf;
if(videoItem ! =nil) {
self.wavePlayer.videoItem = videoItem;
[self.wavePlayer startAnimation];
}
} failureBlock:^(NSError * _Nonnull error) {
}];
}
return _wavePlayer;
}
- (SVGAParser *)waveParser {
if(! _waveParser) { _waveParser = [[SVGAParser alloc] init]; }return _waveParser;
}
@end
Copy the code
- 13. Here I load a local. Svga asking price is the same as the description. You can download materials from the Internet for testing.