YOUPAILZLiveGiftView.m 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. //
  2. // YOUPAILZLiveGiftView.m
  3. // TIANYAN
  4. //
  5. // Created by CY on 2021/5/25.
  6. // Copyright © 2021 leo. All rights reserved.
  7. //
  8. #import "YOUPAILZLiveGiftView.h"
  9. @interface YOUPAILZLiveGiftView ()
  10. @property (nonatomic, weak)UILabel *youpaipcountL;
  11. @property (nonatomic, strong) NSTimer *youpaiptimer;
  12. @property (nonatomic, assign) NSInteger youpaiptimerCount;
  13. @property (nonatomic, strong,readwrite) YOUPAILZLiveGIftModel *youpaipgiftModel;
  14. @end
  15. @implementation YOUPAILZLiveGiftView
  16. + (instancetype)youpaifgiftViewWithModel:(YOUPAILZLiveGIftModel *)model{
  17. return [[self alloc] initWithModel:model];
  18. }
  19. - (instancetype)initWithModel:(YOUPAILZLiveGIftModel *)model{
  20. if (self = [super init]) {
  21. _youpaipgiftModel = model;
  22. [self youpaifinitUI];
  23. }
  24. return self;
  25. }
  26. - (void)youpaifinitUI{
  27. self.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.3f];
  28. self.layer.cornerRadius = 21.0f;
  29. UIImageView *youpaipavatarImgV = [[UIImageView alloc] init];
  30. youpaipavatarImgV.layer.cornerRadius = 17.0f;
  31. youpaipavatarImgV.clipsToBounds = YES;
  32. [youpaipavatarImgV sd_setImageWithURL:[LCTools getImageUrlWithAddress:self.youpaipgiftModel.youpaipavatar]];
  33. [self addSubview:youpaipavatarImgV];
  34. [youpaipavatarImgV mas_makeConstraints:^(MASConstraintMaker *make) {
  35. make.left.top.offset(4.0f);
  36. make.bottom.offset(-4.0f);
  37. make.width.offset(34.0f);
  38. }];
  39. UIImageView *giftImgV = [[UIImageView alloc] init];
  40. [giftImgV sd_setImageWithURL:[LCTools getImageUrlWithAddress:self.youpaipgiftModel.youpaipgiftImage]];
  41. [self addSubview:giftImgV];
  42. [giftImgV mas_makeConstraints:^(MASConstraintMaker *make) {
  43. make.top.offset(4.0f);
  44. make.bottom.right.offset(-4.0f);
  45. make.width.offset(34.0f);
  46. }];
  47. UILabel *youpaipnicknameL = [[UILabel alloc] init];
  48. youpaipnicknameL.font = LCFont(14.0f);
  49. youpaipnicknameL.textColor = [UIColor whiteColor];
  50. youpaipnicknameL.text = self.youpaipgiftModel.youpaipnickName;
  51. youpaipnicknameL.preferredMaxLayoutWidth = 124.0f;
  52. [self addSubview:youpaipnicknameL];
  53. [youpaipnicknameL mas_makeConstraints:^(MASConstraintMaker *make) {
  54. make.bottom.equalTo(self.mas_centerY);
  55. make.left.equalTo(youpaipavatarImgV.mas_right).offset(5.0f);
  56. make.right.equalTo(giftImgV.mas_left).offset(-5.0f);
  57. }];
  58. UILabel *giftDescL = [[UILabel alloc] init];
  59. giftDescL.font = LCFont(14.0f);
  60. giftDescL.textColor = HexColorFromRGB(0xFFF3A7);
  61. giftDescL.text = self.youpaipgiftModel.youpaipgiftDesc;
  62. giftDescL.preferredMaxLayoutWidth = 124.0f;
  63. [self addSubview:giftDescL];
  64. [giftDescL mas_makeConstraints:^(MASConstraintMaker *make) {
  65. make.top.equalTo(self.mas_centerY);
  66. make.left.equalTo(youpaipavatarImgV.mas_right).offset(5.0f);
  67. make.right.equalTo(giftImgV.mas_left).offset(-5.0f);
  68. }];
  69. UILabel *youpaipcountL = [[UILabel alloc] init];
  70. youpaipcountL.font = LCBoldFont(30.0f);
  71. youpaipcountL.textColor = [UIColor whiteColor];
  72. youpaipcountL.text = [NSString stringWithFormat:@"x %@",@(self.youpaipgiftModel.youpaipgiftCount)];
  73. [self addSubview:youpaipcountL];
  74. self.youpaipcountL = youpaipcountL;
  75. [youpaipcountL mas_makeConstraints:^(MASConstraintMaker *make) {
  76. make.top.bottom.equalTo(self);
  77. make.left.equalTo(self.mas_right).offset(4.0f);
  78. }];
  79. }
  80. - (void)youpaifshowAnimation{
  81. [self youpaifstartTimer];
  82. if (self != nil && self.superview != nil) {
  83. [self.superview setNeedsUpdateConstraints];
  84. [self mas_updateConstraints:^(MASConstraintMaker *make) {
  85. make.left.offset(4.0f);
  86. }];
  87. }
  88. [UIView animateWithDuration:0.25f animations:^{
  89. [self.superview layoutIfNeeded];
  90. }];
  91. }
  92. - (void)youpaifhideAnimation{
  93. if (self != nil && self.superview != nil) {
  94. [self.superview setNeedsUpdateConstraints];
  95. [self mas_updateConstraints:^(MASConstraintMaker *make) {
  96. make.left.offset(-200.0f);
  97. }];
  98. }
  99. [UIView animateWithDuration:0.25f animations:^{
  100. [self.superview layoutIfNeeded];
  101. } completion:^(BOOL finished) {
  102. if (finished && [self.delegate respondsToSelector:@selector(youpaifanimatiomFinishWithView:)]) {
  103. [self.delegate youpaifanimatiomFinishWithView:self];
  104. }
  105. }];
  106. }
  107. - (void)youpaifupdateGiftCount:(NSInteger)count{
  108. [self youpaifstopTimer];
  109. self.youpaipgiftModel.youpaipgiftCount += count;
  110. self.youpaipcountL.text = [NSString stringWithFormat:@"x %@",@(self.youpaipgiftModel.youpaipgiftCount)];
  111. [self youpaifstartTimer];
  112. }
  113. - (void)youpaifstartTimer{
  114. self.youpaiptimerCount = 2;
  115. self.youpaiptimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(youpaifcountDown) userInfo:nil repeats:YES];
  116. [[NSRunLoop currentRunLoop] addTimer:self.youpaiptimer forMode:NSRunLoopCommonModes];
  117. }
  118. - (void)youpaifstopTimer{
  119. [self.youpaiptimer invalidate];
  120. self.youpaiptimer = nil;
  121. }
  122. - (void)youpaifcountDown{
  123. self.youpaiptimerCount --;
  124. if (self.youpaiptimerCount <= 0) {
  125. [self youpaifstopTimer];
  126. [self youpaifhideAnimation];
  127. }
  128. }
  129. @end