SendGiftAnimateView.m 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. //
  2. // SendGiftAnimateView.m
  3. // BuguLive
  4. //
  5. // Created by xfg on 16/5/23.
  6. // Copyright © 2016年 xfg. All rights reserved.
  7. //
  8. #import "SendGiftAnimateView.h"
  9. #define kBackGroundHeightRate 0.8
  10. #define kBackGroundWidthRate 0.8
  11. @interface SendGiftAnimateView()<ShakeLabelDelegate>{
  12. UIView *_backGroundView;
  13. UIImageView *_imgView;
  14. NSTimer* _animateDelayTimer; //动画停滞阶段计时
  15. CGFloat _viewY; //视图的Y值
  16. }
  17. @end
  18. @implementation SendGiftAnimateView
  19. - (id)initWithFrame:(CGRect)frame
  20. {
  21. self = [super initWithFrame :frame];
  22. if (self)
  23. {
  24. self.backgroundColor = [UIColor clearColor];
  25. _viewY = frame.origin.y;
  26. _backGroundView = [[UIView alloc]initWithFrame:CGRectMake(-kBackGroundWidthRate*frame.size.width, frame.size.height*(1-kBackGroundHeightRate), frame.size.width*kBackGroundWidthRate, frame.size.height*kBackGroundHeightRate)];
  27. _backGroundView.backgroundColor = [UIColor clearColor];
  28. [self addSubview:_backGroundView];
  29. UIImageView *backGroundImgView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, CGRectGetWidth(_backGroundView.frame), CGRectGetHeight(_backGroundView.frame))];
  30. backGroundImgView.contentMode = UIViewContentModeScaleToFill;
  31. [backGroundImgView setImage:[UIImage imageNamed:@"lr_small_gift_bg"]];
  32. backGroundImgView.clipsToBounds = YES;
  33. [_backGroundView addSubview:backGroundImgView];
  34. _headImgView = [[MenuButton alloc]initWithFrame:CGRectMake(1, 1, _backGroundView.frame.size.height-2, _backGroundView.frame.size.height-2)];
  35. _headImgView.contentMode = UIViewContentModeScaleAspectFill;
  36. _headImgView.layer.cornerRadius = (_backGroundView.frame.size.height-2)/2;
  37. _headImgView.clipsToBounds = YES;
  38. [_backGroundView addSubview:_headImgView];
  39. _titleNameLabel = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(_headImgView.frame)+5, 0, frame.size.width*0.48, _backGroundView.frame.size.height/2)];
  40. _titleNameLabel.textColor = [UIColor whiteColor];
  41. _titleNameLabel.numberOfLines = 1;
  42. _titleNameLabel.font = [UIFont systemFontOfSize:13.0];
  43. [_backGroundView addSubview:_titleNameLabel];
  44. _giftNameLabel = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMinX(_titleNameLabel.frame), CGRectGetMaxY(_titleNameLabel.frame), CGRectGetWidth(_titleNameLabel.frame), _backGroundView.frame.size.height/2)];
  45. _giftNameLabel.textColor = kWhiteColor;
  46. _giftNameLabel.font = [UIFont systemFontOfSize:13.0];
  47. _giftNameLabel.numberOfLines = 1;
  48. [_backGroundView addSubview:_giftNameLabel];
  49. CGFloat giftImgViewWH = self.frame.size.height-5;
  50. _giftImgView = [[UIImageView alloc]initWithFrame:CGRectMake(-giftImgViewWH, 0, giftImgViewWH, giftImgViewWH)];
  51. _giftImgView.contentMode = UIViewContentModeScaleAspectFit;
  52. [self addSubview:_giftImgView];
  53. _numLabel = [[ShakeLabel alloc]initWithFrame:CGRectMake(frame.size.width*kBackGroundWidthRate, 0, frame.size.width*(1-kBackGroundWidthRate), frame.size.height*kBackGroundHeightRate)];
  54. _numLabel.backgroundColor = [UIColor clearColor];
  55. _numLabel.textColor = [UIColor greenColor];
  56. _numLabel.borderColor = [UIColor yellowColor];
  57. _numLabel.font = [UIFont systemFontOfSize:17];
  58. _numLabel.textAlignment = NSTextAlignmentCenter;
  59. _numLabel.delegate = self;
  60. [self addSubview:_numLabel];
  61. self.hidden = YES;
  62. }
  63. return self;
  64. }
  65. - (void)setContent:(CustomMessageModel *)customMessageModel
  66. {
  67. self.customMessageModel = customMessageModel;
  68. if(customMessageModel.type == 1)
  69. {
  70. if (customMessageModel.sender.is_noble_mysterious.intValue == 1) {
  71. _titleNameLabel.text = ASLocalizedString(@"神秘人") ;
  72. [_headImgView sd_setImageWithURL:nil forState:UIControlStateNormal placeholderImage:kDefaultNobleMysteriousHeadImg];
  73. }else{
  74. _titleNameLabel.text = customMessageModel.sender.nick_name;
  75. [_headImgView sd_setImageWithURL:[NSURL URLWithString:customMessageModel.sender.head_image] forState:UIControlStateNormal placeholderImage:kDefaultPreloadHeadImg];
  76. }
  77. _giftNameLabel.text = customMessageModel.desc;
  78. [_giftImgView sd_setImageWithURL:[NSURL URLWithString:customMessageModel.icon] placeholderImage:kDefaultPreloadImgSquare];
  79. _numLabel.text = [NSString stringWithFormat:@"X %ld",(long)customMessageModel.showNum];
  80. _numLabel.textColor = kBlackColor;
  81. _numLabel.borderColor = RGB(255,193,10);
  82. }
  83. else if (customMessageModel.type == 28)
  84. {
  85. // [_headImgView sd_setImageWithURL:[NSURL URLWithString:customMessageModel.user.head_image] forState:UIControlStateNormal placeholderImage:kDefaultPreloadHeadImg];
  86. //
  87. // _titleNameLabel.text = customMessageModel.user.nick_name;
  88. // _giftNameLabel.text = [NSString stringWithFormat:ASLocalizedString(@"参与%@次出价"),customMessageModel.pai_sort];
  89. // [_giftImgView setImage:[UIImage imageNamed:@"ac_hammers"]];
  90. // _numLabel.text = [NSString stringWithFormat:@"X %@",customMessageModel.pai_sort];
  91. // _numLabel.textColor = [UIColor whiteColor];
  92. // _numLabel.borderColor = [UIColor redColor];
  93. }
  94. }
  95. #pragma mark 恢复播放动画前的frame
  96. - (void)recoveryViewFrame
  97. {
  98. _backGroundView.frame = CGRectMake(-kBackGroundWidthRate*self.frame.size.width, _backGroundView.frame.origin.y, _backGroundView.frame.size.width, _backGroundView.frame.size.height);
  99. _giftImgView.frame = CGRectMake(-self.frame.size.height*0.8, _giftImgView.frame.origin.y, _giftImgView.frame.size.width, _giftImgView.frame.size.height);
  100. self.frame = CGRectMake(self.frame.origin.x, _viewY, self.frame.size.width, self.frame.size.height);
  101. if(_animateDelayTimer)
  102. {
  103. [_animateDelayTimer invalidate];
  104. _animateDelayTimer = nil;
  105. }
  106. }
  107. #pragma mark 开始动画
  108. - (BOOL)showGiftAnimate
  109. {
  110. if (!_isPlaying && !_isPlayingDeplay)
  111. {
  112. self.hidden = NO;
  113. [UIView animateWithDuration:0.2 animations:^{
  114. _isPlaying = YES;
  115. _isPlayingDeplay = NO;
  116. _numLabel.hidden = YES;
  117. _backGroundView.frame = CGRectMake(0, _backGroundView.frame.origin.y, _backGroundView.frame.size.width, _backGroundView.frame.size.height);
  118. }completion:^(BOOL finished) {
  119. [UIView animateWithDuration:0.1 animations:^{
  120. _giftImgView.frame = CGRectMake(CGRectGetWidth(_backGroundView.frame)-self.frame.size.height*0.8, 0, _giftImgView.frame.size.width, _giftImgView.frame.size.height);
  121. }];
  122. [self changeFont];
  123. }];
  124. return YES;
  125. }
  126. return NO;
  127. }
  128. #pragma mark 重新开始数字变化
  129. - (BOOL)txtFontAgain
  130. {
  131. if (_isPlaying && _isPlayingDeplay && !_isPlayingTextChanging)
  132. {
  133. if(_animateDelayTimer)
  134. {
  135. [_animateDelayTimer invalidate];
  136. _animateDelayTimer = nil;
  137. }
  138. [self changeFont];
  139. return YES;
  140. }
  141. return NO;
  142. }
  143. #pragma mark 数字由大变小
  144. - (void)changeFont
  145. {
  146. _isPlaying = YES;
  147. _isPlayingDeplay = NO;
  148. _isPlayingTextChanging = YES;
  149. _numLabel.hidden = NO;
  150. [_numLabel startAnimWithDuration:0.25];
  151. }
  152. - (void)shakeLabelAnimateFinished
  153. {
  154. _isPlayingTextChanging = NO;
  155. if(_animateDelayTimer)
  156. {
  157. [_animateDelayTimer invalidate];
  158. _animateDelayTimer = nil;
  159. }
  160. [self displayAfter];
  161. }
  162. - (void)displayAfter
  163. {
  164. _isPlaying = YES;
  165. _isPlayingDeplay = YES;
  166. _animateDelayTimer = [NSTimer scheduledTimerWithTimeInterval:1.8 target:self selector:@selector(viewDisappearAnimate) userInfo:nil repeats:NO];
  167. if (_delegate && [_delegate respondsToSelector:@selector(giftAnimate:)])
  168. {
  169. [_delegate giftAnimate:self];
  170. }
  171. }
  172. #pragma mark 视图消失动画
  173. - (void)viewDisappearAnimate
  174. {
  175. _isPlaying = YES;
  176. _isPlayingDeplay = NO;
  177. [UIView animateWithDuration:0.3 animations:^{
  178. self.frame = CGRectMake(self.frame.origin.x, _viewY-self.frame.size.height, self.frame.size.width, self.frame.size.height);
  179. } completion:^(BOOL finished) {
  180. [self recoveryViewFrame];
  181. _isPlaying = NO;
  182. // self.alpha = 0.0;
  183. self.hidden = YES;
  184. if (_delegate && [_delegate respondsToSelector:@selector(giftAnimate:)]) {
  185. [_delegate giftAnimate:self];
  186. }
  187. }];
  188. }
  189. @end