SendGiftAnimateView2.m 8.1 KB

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