FPKCountDownView.m 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. //
  2. // FPKCountDownView.m
  3. // BuguLive
  4. //
  5. // Created by bogokj on 2019/4/2.
  6. // Copyright © 2019年 xfg. All rights reserved.
  7. //
  8. #import "FPKCountDownView.h"
  9. @implementation FPKCountDownView{
  10. UILabel *labTimer;//定时器
  11. NSTimer *timer;
  12. UIImageView *pkImage;
  13. UIImageView *pkDdate;
  14. //2020-1-3 修改倒计时
  15. UIButton *timeBtn;
  16. }
  17. - (instancetype)initWithFrame:(CGRect)frame liveItem:(nonnull id<FWShowLiveRoomAble>)liveItem{
  18. if (self = [super initWithFrame:frame]) {
  19. _liveItem = liveItem;
  20. [self initSubview];
  21. }
  22. return self;
  23. }
  24. - (void)initSubview{
  25. timeBtn=[[UIButton alloc]init];
  26. [timeBtn setImage:[UIImage imageNamed:@"PK中"] forState:UIControlStateNormal];
  27. timeBtn.titleLabel.font=[UIFont systemFontOfSize:12];
  28. [timeBtn setTitleColor:kWhiteColor forState:UIControlStateNormal];
  29. timeBtn.frame=CGRectMake(kScreenW/2-40, 0, 80, 20);
  30. [self addSubview:timeBtn];
  31. timeBtn.backgroundColor=[kBlackColor colorWithAlphaComponent:0.8];
  32. timeBtn.layer.cornerRadius=10;
  33. timeBtn.layer.masksToBounds=YES;
  34. labTimer = [[UILabel alloc] init];
  35. labTimer.textColor = kWhiteColor;
  36. labTimer.text = @"";
  37. labTimer.font = [UIFont systemFontOfSize:12];
  38. // _countDown = 60;
  39. FWWeakify(self)
  40. timer = [NSTimer bk_scheduledTimerWithTimeInterval:1 block:^(NSTimer *timer) {
  41. FWStrongify(self)
  42. if(_countDown == 0)
  43. {
  44. [timer invalidate];
  45. timer = nil;
  46. [timeBtn setTitle:@"00:00" forState:UIControlStateNormal];
  47. labTimer.text = [NSString stringWithFormat:ASLocalizedString(@"0秒")];
  48. if (_liveItem.liveHost.imUserId.integerValue == [IMAPlatform sharedInstance].host.userId.integerValue) {
  49. //主播
  50. // ctl=pk&act=punishment_time
  51. NSMutableDictionary *dict = [NSMutableDictionary dictionary];
  52. if(![GlobalVariables sharedInstance].openAgora)
  53. {
  54. [dict setObject:@"pk_tencent" forKey:@"ctl"];
  55. }
  56. else
  57. {
  58. [dict setObject:@"pk_agora" forKey:@"ctl"];
  59. }
  60. [dict setObject:@"punishment_time_user" forKey:@"act"];
  61. [dict setObject:_pkid.length ? _pkid : @"" forKey:@"pk_id"];
  62. [dict setObject:[IMAPlatform sharedInstance].host.imUserId forKey:@"user_id"];
  63. [[NetHttpsManager manager] POSTWithParameters:dict SuccessBlock:^(NSDictionary *responseJson) {
  64. if ([responseJson toInt:@"status"] == 1) {
  65. NSLog(@"%s\n%@",__func__,responseJson);
  66. //自己进入惩罚时间
  67. // [[NSNotificationCenter defaultCenter] postNotificationName:kPKChangeToPunish object:responseJson];
  68. NSMutableDictionary *mDict = [NSMutableDictionary dictionary];
  69. if(![GlobalVariables sharedInstance].openAgora)
  70. {
  71. [mDict setObject:@"pk_tencent" forKey:@"ctl"];
  72. }
  73. else
  74. {
  75. [mDict setObject:@"pk_agora" forKey:@"ctl"];
  76. }
  77. [mDict setObject:@"request_get_pk_info2" forKey:@"act"];
  78. [mDict setObject:_pkid.length ? _pkid : @"" forKey:@"pk_id"];
  79. NSDictionary *dic1 = responseJson;
  80. [self.httpsManager POSTWithParameters:mDict SuccessBlock:^(NSDictionary *responseJson) {
  81. if ([responseJson toInt:@"status"] == 1 || [responseJson toInt:@"status"] == 2) {
  82. NSMutableDictionary *dic2 = [NSMutableDictionary dictionaryWithDictionary:responseJson];
  83. [dic2 addEntriesFromDictionary:dic1];
  84. [[NSNotificationCenter defaultCenter] postNotificationName:kPKChangeToPunish object:dic2];
  85. NSLog(ASLocalizedString(@"主播request_get_pk_info2:%@"),responseJson);
  86. }
  87. } FailureBlock:^(NSError *error) {
  88. //do nothing;
  89. }];
  90. //发送给观众消息
  91. // "status": 1,
  92. // "error": "",
  93. // "win_user_id": '获胜者',
  94. // "time": '惩罚时间',
  95. // "act": "punishment_time_user",
  96. // "ctl": "pk_new"
  97. // SendCustomMsgModel *model = [[SendCustomMsgModel alloc]init];
  98. // model.status = 3;
  99. // model.win_user_id = responseJson[@"win_user_id"];
  100. // model.time = responseJson[@"time"];
  101. // model.pk_ticket1 = responseJson[@"pk_ticket1"];
  102. // model.pk_ticket2 = responseJson[@"pk_ticket2"];
  103. //
  104. // model.msgType = MSG_END_PK;
  105. // model.chatGroupID = [_liveItem liveIMChatRoomId];
  106. // [[BGIMMsgHandler sharedInstance] sendCustomGroupMsg:model succ:^{
  107. // NSLog(ASLocalizedString(@"punishment_time_user 消息发送成功"));
  108. // } fail:^(int code, NSString *msg) {
  109. // NSLog(ASLocalizedString(@"punishment_time_user 消息发送失败"));
  110. // }];
  111. }
  112. } FailureBlock:^(NSError *error) {
  113. //do nothing
  114. }];
  115. }else{
  116. // act = "request_get_pk_info2";
  117. // "create_time" = 1555728928;
  118. // ctl = "pk_new";
  119. // "emcee_user_id1" = 10109;
  120. // "emcee_user_id2" = 10103;
  121. // "group_id1" = 32388;
  122. // "group_id2" = 32379;
  123. // "has_focus1" = 0;
  124. // "has_focus2" = 0;
  125. // "head_image1" = "";
  126. // "head_image2" = "";
  127. // id = 46;
  128. // "pk_ticket1" = 0;
  129. // "pk_ticket2" = 0;
  130. // "pk_time" = 60;
  131. // "play_url1" = "rtmp://pull.70tr9.com/live/43062_32388b10109_d62a84db30401e6f63cd";
  132. // "play_url2" = "rtmp://pull.70tr9.com/live/43062_32379b10103_97bf911fb3060b08ef10";
  133. // status = 1;
  134. // time = 1;
  135. //观众
  136. // ctl=pk&act=punishment_time
  137. NSMutableDictionary *dict = [NSMutableDictionary dictionary];
  138. if(![GlobalVariables sharedInstance].openAgora)
  139. {
  140. [dict setObject:@"pk_tencent" forKey:@"ctl"];
  141. }
  142. else
  143. {
  144. [dict setObject:@"pk_agora" forKey:@"ctl"];
  145. }
  146. [dict setObject:@"punishment_time_user" forKey:@"act"];
  147. [dict setObject:_pkid.length ? _pkid : @"" forKey:@"pk_id"];
  148. [dict setObject:[IMAPlatform sharedInstance].host.imUserId forKey:@"user_id"];
  149. [[NetHttpsManager manager] POSTWithParameters:dict SuccessBlock:^(NSDictionary *responseJson) {
  150. if ([responseJson toInt:@"status"] == 1) {
  151. NSLog(@"%s\n%@",__func__,responseJson);
  152. //自己进入惩罚时间
  153. // [[NSNotificationCenter defaultCenter] postNotificationName:kPKChangeToPunish object:responseJson];
  154. NSMutableDictionary *mDict = [NSMutableDictionary dictionary];
  155. if(![GlobalVariables sharedInstance].openAgora)
  156. {
  157. [mDict setObject:@"pk_tencent" forKey:@"ctl"];
  158. }
  159. else
  160. {
  161. [mDict setObject:@"pk_agora" forKey:@"ctl"];
  162. }
  163. [mDict setObject:@"request_get_pk_info2" forKey:@"act"];
  164. [mDict setObject:_pkid.length ? _pkid : @"" forKey:@"pk_id"];
  165. NSDictionary *dic1 = responseJson;
  166. [self.httpsManager POSTWithParameters:mDict SuccessBlock:^(NSDictionary *responseJson) {
  167. if ([responseJson toInt:@"status"] == 1 || [responseJson toInt:@"status"] == 2) {
  168. NSMutableDictionary *dic2 = [NSMutableDictionary dictionaryWithDictionary:responseJson];
  169. [dic2 addEntriesFromDictionary:dic1];
  170. [[NSNotificationCenter defaultCenter] postNotificationName:kPKChangeToPunish object:dic2];
  171. NSLog(ASLocalizedString(@"观众request_get_pk_info2:%@"),dic2);
  172. }
  173. } FailureBlock:^(NSError *error) {
  174. //do nothing;
  175. }];
  176. }
  177. } FailureBlock:^(NSError *error) {
  178. //do nothing
  179. }];
  180. }
  181. }
  182. else
  183. {
  184. _countDown--;
  185. [timeBtn setTitle:[self timeFormatted:_countDown] forState:UIControlStateNormal];
  186. labTimer.text = [NSString stringWithFormat:ASLocalizedString(@"%d秒"),_countDown];
  187. }
  188. } repeats:YES];
  189. // [timeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
  190. // make.width.equalTo(@60);
  191. // make.height.equalTo(@20);
  192. // make.top.equalTo(self);
  193. // make.centerX.equalTo(self);
  194. // }];
  195. pkDdate = [[UIImageView alloc] init];
  196. pkDdate.image = [UIImage imageNamed:@"pkda"];
  197. pkDdate.contentMode = UIViewContentModeScaleAspectFit;
  198. [self addSubview:pkDdate];
  199. [pkDdate mas_makeConstraints:^(MASConstraintMaker *make) {
  200. make.width.equalTo(@38);
  201. make.height.equalTo(@38);
  202. make.top.equalTo(self);
  203. make.centerX.equalTo(self);
  204. }];
  205. [pkDdate addSubview:labTimer];
  206. [labTimer mas_makeConstraints:^(MASConstraintMaker *make) {
  207. make.centerX.equalTo(pkDdate);
  208. make.bottom.equalTo(pkDdate).offset(-3);
  209. }];
  210. if ([_pktype isEqualToString:@"cf"]) {
  211. pkDdate.hidden=NO;
  212. labTimer.hidden=NO;
  213. timeBtn.hidden=YES;
  214. }else{
  215. pkDdate.hidden=YES;
  216. labTimer.hidden=YES;
  217. timeBtn.hidden=NO;
  218. }
  219. }
  220. -(void)setPktype:(NSString *)pktype{
  221. _pktype=pktype;
  222. if ([_pktype isEqualToString:@"cf"]) {
  223. pkDdate.hidden=NO;
  224. labTimer.hidden=NO;
  225. timeBtn.hidden=YES;
  226. }else{
  227. pkDdate.hidden=YES;
  228. labTimer.hidden=YES;
  229. timeBtn.hidden=NO;
  230. }
  231. }
  232. - (void)switchToPunish:(int)time{
  233. _countDown = time;
  234. [self startTimer];
  235. }
  236. - (void)stopTimer {
  237. [timer invalidate];
  238. timer = nil;
  239. }
  240. - (void)startTimer{
  241. [timer invalidate];
  242. timer = nil;
  243. FWWeakify(self)
  244. timer = [NSTimer bk_scheduledTimerWithTimeInterval:1 block:^(NSTimer *timer) {
  245. FWStrongify(self)
  246. if(_countDown == 0)
  247. {
  248. [timer invalidate];
  249. timer = nil;
  250. [timeBtn setTitle:ASLocalizedString(@"结束")forState:UIControlStateNormal];
  251. labTimer.text = [NSString stringWithFormat:ASLocalizedString(@"结束")];
  252. if ([_pktype isEqualToString:@"cf"]) {
  253. pkDdate.hidden=NO;
  254. labTimer.hidden=NO;
  255. timeBtn.hidden=YES;
  256. }else{
  257. pkDdate.hidden=YES;
  258. labTimer.hidden=YES;
  259. timeBtn.hidden=NO;
  260. }
  261. [[NSNotificationCenter defaultCenter] postNotificationName:@"endPunish" object:nil];
  262. }
  263. else
  264. {
  265. _countDown--;
  266. [timeBtn setTitle:[self timeFormatted:_countDown] forState:UIControlStateNormal];
  267. [timeBtn setImage:[UIImage imageNamed:@"PK中"] forState:UIControlStateNormal];
  268. labTimer.text = [NSString stringWithFormat:ASLocalizedString(@"%d秒"),_countDown];
  269. [pkDdate setImage:[UIImage imageNamed:@"bg_pk_punish_time"]];
  270. }
  271. } repeats:YES];
  272. }
  273. - (void)dealloc{
  274. NSLog(@"%@ dealloc",NSStringFromClass([FPKCountDownView class]));
  275. if (timer) {
  276. [timer invalidate];
  277. timer = nil;
  278. }
  279. }
  280. - (NSString *)timeFormatted:(int)totalSeconds
  281. {
  282. int seconds = totalSeconds % 60;
  283. int minutes = (totalSeconds / 60) % 60;
  284. return [NSString stringWithFormat:@"%02d:%02d", minutes, seconds];
  285. }
  286. @end