SVGAAnimate.m 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. //
  2. // SVGAAnimate.m
  3. // FanweApp
  4. //
  5. // Created by 志刚杨 on 2017/12/26.
  6. // Copyright © 2017年 voidcat. All rights reserved.
  7. //
  8. #import "SVGAAnimate.h"
  9. static SVGAParser *parser;
  10. @interface SVGAAnimate ()<SVGAPlayerDelegate>
  11. {
  12. CGFloat _top;
  13. NSString *_senderName;
  14. NSURL *svgUrl;
  15. UIImageView *rankImgView;
  16. UILabel *userNameLabel;
  17. UILabel *contentlab;
  18. }
  19. @property (nonatomic, weak)IBOutlet FLAnimatedImageView *gifImage;
  20. @property (nonatomic, weak)IBOutlet UILabel *nickLabel;
  21. @property (nonatomic, assign) NSUInteger currentLoopIndex; // 当前循环的次数
  22. @property (nonatomic, strong) SVGAPlayer *aPlayer;
  23. @end
  24. @implementation SVGAAnimate
  25. - (id)initWithModel:(CustomMessageModel*)gift inView:(UIView*)superView andSenderName:(NSString *)senderName
  26. {
  27. self = [[[NSBundle mainBundle] loadNibNamed:@"SVGAAnimate" owner:self options:nil] lastObject];
  28. if (self)
  29. {
  30. self.backgroundColor = [UIColor clearColor];
  31. _nickLabel.hidden = YES;
  32. [superView addSubview:self];
  33. [superView sendSubviewToBack:self];
  34. _senderName = senderName;
  35. [self cfgWithGift:gift andTop:0];
  36. }
  37. return self;
  38. }
  39. + (void)showGift:(CustomMessageModel *)gift inVc:(UIViewController *)vc
  40. {
  41. // SVGAAnimate *xx = [[NSBundle mainBundle] loadNibNamed:@"SVGAAnimate" owner:nil options:nil].firstObject;
  42. // xx.backgroundColor = [UIColor clearColor];
  43. // [vc.view addSubview:xx];
  44. //
  45. // [xx cfgWithGift:gift andTop:0];
  46. }
  47. - (void)cfgWithGift:(CustomMessageModel*)gift andTop:(CGFloat)top
  48. {
  49. _giftItem = gift;
  50. _top = top;
  51. __weak typeof(self) ws = self;
  52. parser = [[SVGAParser alloc] init];
  53. [parser parseWithURL:[NSURL URLWithString:gift.animated_url] completionBlock:^(SVGAVideoEntity * _Nullable videoItem) {
  54. self.aPlayer.videoItem = videoItem;
  55. dispatch_async(dispatch_get_main_queue(), ^{
  56. [ws setUpView:gift];
  57. });
  58. //图片下载完成 在这里进行相关操作,如加到数组里 或者显示在imageView上
  59. // if (ws.giftItem.delay_time)
  60. // {
  61. // ws.hidden = YES;
  62. // [ws performSelector:@selector(setUpView:) withObject:gift afterDelay:_giftItem.delay_time/1000];
  63. // }
  64. // else
  65. // {
  66. // [ws setUpView:gift];
  67. // }
  68. } failureBlock:^(NSError * _Nullable error) {
  69. [[BGHUDHelper sharedInstance] tipMessage:[NSString stringWithFormat:ASLocalizedString(@"svga播放错误%@"),error]];
  70. }];
  71. }
  72. - (void)setUpView:(CustomMessageModel*)gift
  73. {
  74. self.hidden = NO;
  75. [self setUserInteractionEnabled:NO];
  76. CGRect pRect = self.superview.frame; //superview的frame
  77. CGRect vRect = self.gifImage.frame; //gifImage的frame
  78. CGSize size = self.gifImage.currentFrame.size; //图片的size
  79. CGFloat imageScale = size.width/size.height; //图片宽高比
  80. CGFloat w = size.width;
  81. CGFloat h = size.height;
  82. if ((size.width-pRect.size.width) > 0 && (size.height-pRect.size.height) > 0)
  83. { //如果图片的宽、高都超过的屏幕宽、高
  84. if ((size.width-pRect.size.width) > (size.height-pRect.size.height))
  85. {
  86. w = pRect.size.width;
  87. h = w / imageScale;
  88. }
  89. else
  90. {
  91. h = pRect.size.height;
  92. w = h * imageScale;
  93. }
  94. }
  95. else if ((size.width-pRect.size.width) > 0 && (size.height-pRect.size.height) < 0)
  96. { //如果图片的宽超过的屏幕宽,但是高小于屏幕的高
  97. w = pRect.size.width;
  98. h = w / imageScale;
  99. }
  100. else if ((size.width-pRect.size.width) < 0 && (size.height-pRect.size.height) > 0)
  101. { //如果图片的高超过的屏幕高,但是宽小于屏幕的宽
  102. h = pRect.size.height;
  103. w = h * imageScale;
  104. }
  105. vRect.size = CGSizeMake(w, h);
  106. self.gifImage.frame = vRect;
  107. size = vRect.size;
  108. // if (_giftItem.show_user == 0)
  109. // {
  110. // _nickLabel.hidden = YES;
  111. // _nickLabel.text = @"";
  112. // }
  113. // else
  114. // {
  115. _nickLabel.hidden = NO;
  116. [self bringSubviewToFront:_nickLabel];
  117. size.height += _nickLabel.frame.size.height;
  118. _nickLabel.text = _senderName;
  119. // }
  120. CGRect rect = CGRectMake((pRect.size.width-size.width)/2, _top, size.width, size.height);
  121. //0:使用path路径;1:屏幕上部;2:屏幕中间;3:屏幕底部
  122. if (_giftItem.type == 0)
  123. {
  124. }
  125. else if(_giftItem.type == 1)
  126. {
  127. rect.origin.y = 0;
  128. }
  129. else if(_giftItem.type == 2)
  130. {
  131. rect.origin.y = (pRect.size.height - size.height)/2;
  132. }
  133. else if(_giftItem.type == 3)
  134. {
  135. rect.origin.y = pRect.size.height - size.height;
  136. }
  137. self.frame = CGRectMake(0, 0, kScreenW, kScreenH);
  138. self.aPlayer.delegate = self;
  139. self.aPlayer.frame = self.bounds;
  140. self.aPlayer.loops = 1;
  141. self.aPlayer.clearsAfterStop = YES;
  142. [self addSubview:self.aPlayer];
  143. [self layoutIfNeeded];
  144. [self.aPlayer startAnimation];
  145. }
  146. -(void)svgaPlayerDidFinishedAnimation:(SVGAPlayer *)player
  147. {
  148. [self dismissSelf];
  149. }
  150. - (void)dismissSelf
  151. {
  152. [UIView animateWithDuration:0.01 animations:^{
  153. self.alpha = 0.0;
  154. } completion:^(BOOL finished) {
  155. [self removeFromSuperview];
  156. if (_delegate && [_delegate respondsToSelector:@selector(gifImageViewFinish:andSenderName:)]) {
  157. // _giftItem.isFinishAnimate = YES;
  158. [_delegate SVGAViewFinish:_giftItem andSenderName:_senderName];
  159. // [_delegate gifImageViewFinish:_giftItem andSenderName:_senderName];
  160. }
  161. }];
  162. }
  163. - (SVGAPlayer *)aPlayer {
  164. if (_aPlayer == nil) {
  165. _aPlayer = [[SVGAPlayer alloc] init];
  166. _aPlayer.contentMode = UIViewContentModeScaleAspectFit;
  167. }
  168. return _aPlayer;
  169. }
  170. @end