问题代码
- (void)showAnimation
{
if (![TPSkinManager shareManager].isHaveSkin ||
[TPUserDefault instance].isNightMode.intValue == 1 ||
![TPSkinManager showTopAnimation]) {
return;
}
dispatch_async(dispatch_get_main_queue(), ^{
self.svgPlayer.hidden = NO;
NSString *topAnimationName = [[TPSkinManager shareManager] getSkinInfoListVauleWithKey:TPSkinMapKey_animation_topbar];
SVGAParser *parser = [[SVGAParser alloc] init];
NSString *path = [NSString stringWithFormat:@"%@/%@/%@",kSkinCachePath,[TPUserDefault instance].skinPath,topAnimationName];
NSData *data = [NSData dataWithContentsOfFile:path];
[parser parseWithData:data cacheKey:@"" completionBlock:^(SVGAVideoEntity * _Nonnull videoItem) {
self.svgPlayer.videoItem = videoItem;
} failureBlock:^(NSError * _Nonnull error) {
}];
});
问题原因,异步延迟
在调用动画方法的时候才解析动画文件, 而解析动画文件是异步的
解决方法,提前解析号文件数据,调用动画的时候直接开始动画
- (SVGAPlayer *)svgPlayer
{
if (!_svgPlayer) {
_svgPlayer = [[SVGAPlayer alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, SafeAreaTopHeight + 44 * rectScale())];
_svgPlayer.hidden = YES;
_svgPlayer.loops = 1;
_svgPlayer.clearsAfterStop = NO;
[self configSvgaDataWithCompletion:nil];
}
return _svgPlayer;
}
- (void)configSvgaDataWithCompletion:(void (^)(void))completion
{
NSString *topAnimationName = [[TPSkinManager shareManager] getSkinInfoListVauleWithKey:TPSkinMapKey_animation_topbar];
SVGAParser *parser = [[SVGAParser alloc] init];
NSString *path = [NSString stringWithFormat:@"%@/%@/%@",kSkinCachePath,[TPUserDefault instance].skinPath,topAnimationName];
NSData *data = [NSData dataWithContentsOfFile:path];
[parser parseWithData:data cacheKey:@"" completionBlock:^(SVGAVideoEntity * _Nonnull videoItem) {
_svgPlayer.videoItem = videoItem;
if (completion) {
completion();
}
} failureBlock:^(NSError * _Nonnull error) {
}];
}
- (void)showAnimation
{
dispatch_async(dispatch_get_main_queue(), ^{
self.svgPlayer.hidden = NO;
if (self.svgPlayer.videoItem) {
[self.svgPlayer startAnimation];
} else {
[self configSvgaDataWithCompletion:^{
[self.svgPlayer startAnimation];
}];
}
});
}