SVGAAnimationView.m 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. //
  2. // SVGAAnimationView.m
  3. // SVGADemo
  4. //
  5. // Created by bogokj on 2020/10/11.
  6. //
  7. #import "SVGAAnimationView.h"
  8. #import "SVGA.h"
  9. #import "SVGAVideoEntity.h"
  10. #import "GiftModel.h"
  11. @interface SVGAAnimationView ()<SVGAPlayerDelegate>
  12. @property(nonatomic, strong) SVGAPlayer *player;
  13. @property(nonatomic, strong) SVGAParser *parser;
  14. @property(nonatomic, strong) UILabel *contentLabel;
  15. @end
  16. @implementation SVGAAnimationView
  17. - (instancetype)initWithFrame:(CGRect)frame{
  18. if (self = [super initWithFrame:frame]) {
  19. [self setUserInteractionEnabled:NO];
  20. [self addSubview:self.player];
  21. [self addSubview:self.contentLabel];
  22. [self.contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  23. make.left.right.equalTo(self);
  24. make.bottom.equalTo(self).offset(- MG_BOTTOM_SAFE_HEIGHT - 49 - 20);
  25. }];
  26. }
  27. return self;
  28. }
  29. - (void)setGiftModel:(GiftModel *)giftModel{
  30. _giftModel = giftModel;
  31. NSString *url;
  32. url = giftModel.animated_url;
  33. [self.parser parseWithURL:[NSURL URLWithString:url] completionBlock:^(SVGAVideoEntity * _Nullable videoItem) {
  34. if (videoItem != nil) {
  35. dispatch_async(dispatch_get_main_queue(), ^{
  36. CGSize newSize = CGSizeMake(kScreenW, kScreenW * videoItem.videoSize.height / videoItem.videoSize.width);
  37. self.player.frame = CGRectMake(0, (kScreenHeight - newSize.height ) / 2, newSize.width, newSize.height);
  38. [self.contentLabel mas_updateConstraints:^(MASConstraintMaker *make) {
  39. make.top.equalTo(self.player.mas_bottom).offset(10);
  40. }];
  41. self.player.videoItem = videoItem;
  42. [self.player startAnimation];
  43. });
  44. }
  45. } failureBlock:nil];
  46. }
  47. - (void)svgaPlayerDidFinishedAnimation:(SVGAPlayer *)player{
  48. if (self.delegate && [self.delegate respondsToSelector:@selector(svgaAnimationView:didFinishAnimation:)]) {
  49. [self.delegate svgaAnimationView:self didFinishAnimation:self.giftModel];
  50. }
  51. }
  52. - (SVGAPlayer *)player{
  53. if (!_player) {
  54. _player = [[SVGAPlayer alloc] initWithFrame:self.bounds];
  55. _player.delegate = self;
  56. _player.loops = 1;
  57. _player.clearsAfterStop = YES;
  58. }
  59. return _player;
  60. }
  61. - (UILabel *)contentLabel{
  62. if (!_contentLabel) {
  63. _contentLabel = [[UILabel alloc]initWithFrame:CGRectZero];
  64. _contentLabel.font = [UIFont systemFontOfSize:15];
  65. _contentLabel.textColor = [UIColor colorWithHexString:@"ffffff"];
  66. _contentLabel.textAlignment = NSTextAlignmentCenter;
  67. }
  68. return _contentLabel;
  69. }
  70. - (SVGAParser *)parser{
  71. if (!_parser) {
  72. _parser = [[SVGAParser alloc] init];
  73. }
  74. return _parser;
  75. }
  76. @end