YOUPAILZPKDataView.m 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. //
  2. // YOUPAILZPKDataView.m
  3. // TIANYAN
  4. //
  5. // Created by CY on 2021/6/1.
  6. // Copyright © 2021 leo. All rights reserved.
  7. //
  8. #import "YOUPAILZPKDataView.h"
  9. #import "GGProgressView.h"
  10. @interface YOUPAILZPKDataView ()
  11. @property (nonatomic,weak) UIImageView *youpaipimgV1; // 赢家 or 输家
  12. @property (nonatomic,weak) UIImageView *youpaipimgV2; // 赢家 or 输家
  13. @property (nonatomic,weak) UIImageView *youpaiptimerBgV; // 倒计时背景
  14. @property (nonatomic,weak) UIImageView *youpaiptimerFrontImgV;
  15. @property (nonatomic,weak) UILabel *youpaiptimerL; // 倒计时
  16. @property (nonatomic,weak) GGProgressView *youpaipprogressView; // 进度条
  17. @property (nonatomic,weak) UILabel *youpaipweL; // 我方
  18. @property (nonatomic,weak) UILabel *youpaipenemyL; // 对方
  19. @property (nonatomic,weak) UIImageView *youpaipprogressImgV; // 进度
  20. @property (nonatomic,assign) NSInteger youpaippkTime; // pk剩余时长
  21. @property (nonatomic,assign) NSInteger youpaippunishTimer; // 惩罚剩余时长
  22. @property (nonatomic,strong) NSTimer *youpaiptimer; //计时器
  23. @end
  24. @implementation YOUPAILZPKDataView
  25. - (instancetype)initWithFrame:(CGRect)frame model:(YOUPAILZLiveModel *)model{
  26. if (self = [super initWithFrame:frame]) {
  27. _youpaipmodel = model;
  28. self.hidden = YES;
  29. self.youpaipisStartPK = NO;
  30. [self youpaifinitUI];
  31. self.youpaiptimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(youpaifcountDown) userInfo:nil repeats:YES];
  32. [[NSRunLoop currentRunLoop] addTimer:self.youpaiptimer forMode:NSRunLoopCommonModes];
  33. }
  34. return self;
  35. }
  36. - (void)youpaifinitUI{
  37. UIImageView *imgV1 = [[UIImageView alloc] init];
  38. imgV1.hidden = YES;
  39. [self addSubview:imgV1];
  40. self.youpaipimgV1 = imgV1;
  41. [imgV1 mas_makeConstraints:^(MASConstraintMaker *make) {
  42. make.left.offset(0.0f);
  43. make.top.offset(-21.0f);
  44. make.height.offset(48.0f);
  45. }];
  46. UIImageView *imgV2 = [[UIImageView alloc] init];
  47. imgV2.hidden = YES;
  48. [self addSubview:imgV2];
  49. self.youpaipimgV2 = imgV2;
  50. [imgV2 mas_makeConstraints:^(MASConstraintMaker *make) {
  51. make.right.offset(0.0f);
  52. make.top.offset(-21.0f);
  53. make.height.offset(48.0f);
  54. }];
  55. UIImageView *youpaiptimerBgV = [[UIImageView alloc] init];
  56. youpaiptimerBgV.image = [UIImage imageNamed:@"vqu_images_L_live_pk_count_down_border"];
  57. [self addSubview:youpaiptimerBgV];
  58. self.youpaiptimerBgV = youpaiptimerBgV;
  59. [youpaiptimerBgV mas_makeConstraints:^(MASConstraintMaker *make) {
  60. make.centerX.equalTo(self.mas_centerX);
  61. make.top.offset(9.5f);
  62. make.size.mas_offset(CGSizeMake(100.0f, 21.0f));
  63. }];
  64. UIImageView *youpaiptimerFrontImgV = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"vqu_images_L_live_pk_count_down_timer_front_pk"]];
  65. [youpaiptimerBgV addSubview:youpaiptimerFrontImgV];
  66. self.youpaiptimerFrontImgV = youpaiptimerFrontImgV;
  67. [youpaiptimerFrontImgV mas_makeConstraints:^(MASConstraintMaker *make) {
  68. make.left.offset(22.0f);
  69. make.centerY.equalTo(youpaiptimerBgV);
  70. make.size.mas_offset(CGSizeMake(21.0f, 17.0f));
  71. }];
  72. UILabel *youpaiptimerL = [[UILabel alloc] init];
  73. youpaiptimerL.text = @"00:00";
  74. youpaiptimerL.font = LCFont(10.0f);
  75. youpaiptimerL.textColor = [UIColor whiteColor];
  76. youpaiptimerL.textAlignment = NSTextAlignmentCenter;
  77. [youpaiptimerBgV addSubview:youpaiptimerL];
  78. self.youpaiptimerL = youpaiptimerL;
  79. [youpaiptimerL mas_makeConstraints:^(MASConstraintMaker *make) {
  80. make.left.equalTo(youpaiptimerFrontImgV.mas_right).offset(5.0f);
  81. make.right.offset(-22.0f);
  82. make.top.bottom.offset(0.0f);
  83. }];
  84. GGProgressView *progressView = [[GGProgressView alloc] initWithFrame:CGRectMake(5.0f, 33.0f, self.mj_w - 10.0f, 19.0f)];
  85. progressView.progress = 0.5f;
  86. progressView.progressTintColor = [UIColor colorWithPatternImage:[LCTools ColorImage:CGSizeMake(KScreenWidth, 19.0f) FromColors:@[HexColorFromRGB(0xFC2F55),HexColorFromRGB(0xFF3BDC)] ByGradientType:GradientLeftToRight]];
  87. progressView.trackTintColor = [UIColor colorWithPatternImage:[LCTools ColorImage:CGSizeMake(KScreenWidth, 19.0f) FromColors:@[HexColorFromRGB(0x69BCF8),HexColorFromRGB(0x274BE8)] ByGradientType:GradientLeftToRight]];
  88. progressView.layer.cornerRadius = 2.0f;
  89. progressView.clipsToBounds = YES;
  90. [self addSubview:progressView];
  91. self.youpaipprogressView = progressView;
  92. UILabel *weL = [[UILabel alloc] init];
  93. weL.text = @"我方 0";
  94. weL.font = LCFont(12.0f);
  95. weL.textColor = [UIColor whiteColor];
  96. [progressView addSubview:weL];
  97. self.youpaipweL = weL;
  98. [weL mas_makeConstraints:^(MASConstraintMaker *make) {
  99. make.left.offset(7.0f);
  100. make.top.bottom.offset(0.0f);
  101. }];
  102. UILabel *youpaipenemyL = [[UILabel alloc] init];
  103. youpaipenemyL.text = @"0 对方";
  104. youpaipenemyL.font = LCFont(12.0f);
  105. youpaipenemyL.textColor = [UIColor whiteColor];
  106. [progressView addSubview:youpaipenemyL];
  107. self.youpaipenemyL = youpaipenemyL;
  108. [youpaipenemyL mas_makeConstraints:^(MASConstraintMaker *make) {
  109. make.right.offset(-7.0f);
  110. make.top.bottom.offset(0.0f);
  111. }];
  112. UIImageView *youpaipprogressImgV = [[UIImageView alloc] initWithFrame:CGRectMake((progressView.mj_w - 19.0f) / 2.0f, -6, 19.0f, 31.0f)];
  113. youpaipprogressImgV.image = [UIImage imageNamed:@"vqu_images_L_live_pk_progress"];
  114. [progressView addSubview:youpaipprogressImgV];
  115. self.youpaipprogressImgV = youpaipprogressImgV;
  116. }
  117. - (void)youpaifreloadWithAttachment:(YOUPAILZLivePKDataAttachment *)attachment{
  118. self.youpaipattachment = attachment;
  119. NSInteger currentTime = [attachment.time integerValue];
  120. NSInteger pkEndTime = [attachment.pk_end_time integerValue];
  121. NSInteger punishEndTime = [attachment.punish_end_time integerValue];
  122. self.youpaippkTime = pkEndTime - currentTime;
  123. if (currentTime > pkEndTime) {
  124. self.youpaippunishTimer = punishEndTime - currentTime;
  125. }else{
  126. self.youpaippunishTimer = punishEndTime - pkEndTime;
  127. }
  128. NSInteger weNum = 0; // 我方
  129. NSInteger enemyNum = 0; // 对方
  130. if ([self.youpaipmodel.youpaipuser_info.youpaipuser_id isEqual:attachment.from_uid]) {
  131. weNum = [attachment.from_num integerValue];
  132. enemyNum = [attachment.to_num integerValue];
  133. }else{
  134. weNum = [attachment.to_num integerValue];
  135. enemyNum = [attachment.from_num integerValue];
  136. }
  137. if (self.youpaippkTime > 0) {
  138. self.youpaipisStartPK = YES;
  139. self.youpaiptimerFrontImgV.image = [UIImage imageNamed:@"vqu_images_L_live_pk_count_down_timer_front_pk"];
  140. self.youpaiptimerL.text = [NSString stringWithFormat:@"%@",[self youpaiftimeFormatted:self.youpaippkTime]];
  141. self.youpaipimgV1.hidden = YES;
  142. self.youpaipimgV2.hidden = YES;
  143. if (self.currentPKStateBlock != nil) {
  144. self.currentPKStateBlock(LZLivePKStatePK);
  145. }
  146. if (self.showRePKBlock != nil) {
  147. self.showRePKBlock(NO);
  148. }
  149. if (self.showDrawBlock) {
  150. self.showDrawBlock(NO);
  151. }
  152. }else{
  153. self.youpaipimgV1.hidden = NO;
  154. self.youpaipimgV2.hidden = NO;
  155. if (self.showRePKBlock != nil && self.youpaipisStartPK) {
  156. self.showRePKBlock(YES);
  157. }
  158. }
  159. if (self.youpaippkTime <= 0 && self.youpaippunishTimer > 0) {
  160. self.youpaiptimerFrontImgV.image = [UIImage imageNamed:@"vqu_images_L_live_pk_count_down_timer_front_chenfa"];
  161. self.youpaiptimerL.text = [NSString stringWithFormat:@"%@",[self youpaiftimeFormatted:self.youpaippunishTimer]];
  162. if (self.currentPKStateBlock != nil) {
  163. self.currentPKStateBlock(LZLivePKStatePunishment);
  164. }
  165. if (weNum == enemyNum && self.showDrawBlock) {
  166. self.showDrawBlock(YES);
  167. }
  168. }
  169. if (self.youpaippkTime <= 0 && self.youpaippunishTimer <= 0) {
  170. if (self.currentPKStateBlock != nil) {
  171. self.currentPKStateBlock(LZLivePKStateEnd);
  172. }
  173. if (self.showDrawBlock) {
  174. self.showDrawBlock(NO);
  175. }
  176. self.youpaipattachment = nil;
  177. self.hidden = YES;
  178. return;
  179. }
  180. self.youpaipweL.text = [NSString stringWithFormat:@"我方 %@",@(weNum)];
  181. self.youpaipenemyL.text = [NSString stringWithFormat:@"%@ 对方",@(enemyNum)];
  182. if (weNum == 0 && enemyNum == 0) {
  183. self.youpaipprogressView.progress = 0.5f;
  184. self.youpaipprogressImgV.frame = CGRectMake((self.youpaipprogressView.mj_w - 19.0f) / 2.0f, -6, 19.0f, 31.0f);
  185. }else{
  186. CGFloat total = (CGFloat)weNum + (CGFloat)enemyNum;
  187. CGFloat progress = weNum / total;
  188. self.youpaipprogressView.progress = progress;
  189. CGFloat x = self.youpaipprogressView.mj_w * progress - 9.0f;
  190. self.youpaipprogressImgV.frame = CGRectMake(x, -6, 19.0f, 31.0f);
  191. }
  192. if(weNum < enemyNum){
  193. self.youpaipimgV1.image = [UIImage imageNamed:@"vqu_images_L_live_pk_lose"];
  194. self.youpaipimgV2.image = [UIImage imageNamed:@"vqu_images_L_live_pk_win"];
  195. if (self.showDrawBlock) {
  196. self.showDrawBlock(NO);
  197. }
  198. }else if (weNum > enemyNum){
  199. self.youpaipimgV2.image = [UIImage imageNamed:@"vqu_images_L_live_pk_lose"];
  200. self.youpaipimgV1.image = [UIImage imageNamed:@"vqu_images_L_live_pk_win"];
  201. if (self.showDrawBlock) {
  202. self.showDrawBlock(NO);
  203. }
  204. }else{
  205. self.youpaipimgV1.hidden = YES;
  206. self.youpaipimgV2.hidden = YES;
  207. }
  208. }
  209. - (void)youpaifcountDown{
  210. NSInteger weNum = 0;
  211. NSInteger enemyNum = 0;
  212. if ([self.youpaipmodel.youpaipuser_info.youpaipuser_id isEqual:self.youpaipattachment.from_uid]) {
  213. weNum = [self.youpaipattachment.from_num integerValue];
  214. enemyNum = [self.youpaipattachment.to_num integerValue];
  215. }else{
  216. weNum = [self.youpaipattachment.to_num integerValue];
  217. enemyNum = [self.youpaipattachment.from_num integerValue];
  218. }
  219. if (self.youpaippkTime > 0) {
  220. self.youpaippkTime --;
  221. self.youpaiptimerFrontImgV.image = [UIImage imageNamed:@"vqu_images_L_live_pk_count_down_timer_front_pk"];
  222. self.youpaiptimerL.text = [NSString stringWithFormat:@"%@",[self youpaiftimeFormatted:self.youpaippkTime]];
  223. self.youpaipimgV1.hidden = YES;
  224. self.youpaipimgV2.hidden = YES;
  225. if (self.currentPKStateBlock != nil) {
  226. self.currentPKStateBlock(LZLivePKStatePK);
  227. }
  228. if (self.showRePKBlock != nil) {
  229. self.showRePKBlock(NO);
  230. }
  231. }else{
  232. self.youpaipimgV1.hidden = NO;
  233. self.youpaipimgV2.hidden = NO;
  234. if (self.showRePKBlock != nil && self.youpaipisStartPK) {
  235. self.showRePKBlock(YES);
  236. }
  237. }
  238. if (self.youpaippkTime <= 0 && self.youpaippunishTimer > 0) {
  239. self.youpaippunishTimer --;
  240. self.youpaiptimerFrontImgV.image = [UIImage imageNamed:@"vqu_images_L_live_pk_count_down_timer_front_chenfa"];
  241. self.youpaiptimerL.text = [NSString stringWithFormat:@"%@",[self youpaiftimeFormatted:self.youpaippunishTimer]];
  242. if (self.currentPKStateBlock != nil) {
  243. self.currentPKStateBlock(LZLivePKStatePunishment);
  244. }
  245. if (self.showDrawBlock) {
  246. if (weNum == enemyNum) {
  247. self.showDrawBlock(self.youpaipattachment != nil);
  248. }else{
  249. self.showDrawBlock(NO);
  250. }
  251. }
  252. }
  253. if (self.youpaippkTime <= 0 && self.youpaippunishTimer <= 0) {
  254. if (self.currentPKStateBlock != nil) {
  255. self.currentPKStateBlock(LZLivePKStateEnd);
  256. }
  257. if (self.showDrawBlock) {
  258. self.showDrawBlock(NO);
  259. }
  260. self.youpaipattachment = nil;
  261. self.hidden = YES;
  262. return;
  263. }
  264. if(weNum < enemyNum){
  265. self.youpaipimgV1.image = [UIImage imageNamed:@"vqu_images_L_live_pk_lose"];
  266. self.youpaipimgV2.image = [UIImage imageNamed:@"vqu_images_L_live_pk_win"];
  267. }else if (weNum > enemyNum){
  268. self.youpaipimgV2.image = [UIImage imageNamed:@"vqu_images_L_live_pk_lose"];
  269. self.youpaipimgV1.image = [UIImage imageNamed:@"vqu_images_L_live_pk_win"];
  270. }else{
  271. self.youpaipimgV1.hidden = YES;
  272. self.youpaipimgV2.hidden = YES;
  273. }
  274. }
  275. - (NSString *)youpaiftimeFormatted:(NSInteger)totalSeconds{
  276. NSInteger seconds = totalSeconds % 60;
  277. NSInteger minutes = (totalSeconds / 60);
  278. return [NSString stringWithFormat:@"%02ld:%02ld", minutes, seconds];
  279. }
  280. - (void)dealloc{
  281. [self youpaifstopTimer];
  282. }
  283. - (void)youpaifstopTimer{
  284. [self.youpaiptimer invalidate];
  285. self.youpaiptimer = nil;
  286. }
  287. @end