PKPopView.m 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. //
  2. // PKPopView.m
  3. // BuguLive
  4. //
  5. // Created by 范东 on 2019/1/24.
  6. // Copyright © 2019 xfg. All rights reserved.
  7. //
  8. #import "PKPopView.h"
  9. #import "PKTimeModel.h"
  10. #import <QMUIButton.h>
  11. #define kPKPopViewTimePrefix @""
  12. //ASLocalizedString(@"PK时长 ")
  13. @interface PKPopView()<UITableViewDelegate,UITableViewDataSource>
  14. @property (nonatomic, assign) PKPopViewType type;
  15. @property (nonatomic, strong) NSMutableArray *dataArray;
  16. @property (nonatomic, strong) UITableView *tableView;
  17. @property (nonatomic, strong) UIView *shadowView;
  18. @property (nonatomic, strong) UIImageView *leftImageView;
  19. @property (nonatomic, strong) UIImageView *rightImageView;
  20. @property (nonatomic, strong) UILabel *timeLabel;
  21. @property(nonatomic, strong) QMUIButton *timeBtn;
  22. @property (nonatomic, copy) clickTimeCellBlock clickTimeCellBlock;
  23. @property (nonatomic, copy) clickSetTimeBlcok clickSetTimeBlcok;
  24. @property (nonatomic, copy) clickPkBtnBlock clickPkBtnBlock;
  25. @property (nonatomic, copy) clickEndPkBtnBlock clickEndPkBtnBlock;
  26. @end
  27. @implementation PKPopView
  28. - (instancetype)initWithType:(PKPopViewType)type{
  29. if (self = [super init]) {
  30. _type = type;
  31. [self initSubview];
  32. }
  33. return self;
  34. }
  35. - (void)initSubview{
  36. switch (_type) {
  37. case PKPopViewTypeInvication:
  38. {
  39. [self requestPKListData];
  40. UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, kScreenW, 44)];
  41. titleLabel.textColor = kBlackColor;
  42. titleLabel.textAlignment = NSTextAlignmentCenter;
  43. titleLabel.text = ASLocalizedString(@"设置PK时长")
  44. ;
  45. titleLabel.font = [UIFont systemFontOfSize:17];
  46. [self addSubview:titleLabel];
  47. UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(0, 43.5, kScreenW, 0.5)];
  48. lineView.backgroundColor = kBlackColor;
  49. [self addSubview:lineView];
  50. // UILabel *timeLabel = [[UILabel alloc]initWithFrame:CGRectMake(100, lineView.bottom + 20, kScreenW - 200, 30)];
  51. // timeLabel.font = [UIFont systemFontOfSize:15];
  52. // timeLabel.textColor = kWhiteColor;
  53. // [self addSubview:timeLabel];
  54. // self.timeLabel = timeLabel;
  55. // UIButton *timeBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, kRealValue(75), kRealValue(200), kRealValue(40))];
  56. QMUIButton *timeBtn = [QMUIButton buttonWithType:UIButtonTypeCustom];
  57. timeBtn.backgroundColor = [UIColor colorWithHexString:@"#DCDCDC"];
  58. // [UIColor colorWithHexString:@"#28292F"];
  59. timeBtn.frame = CGRectMake(0, 75, 200, 40);
  60. timeBtn.centerX = kScreenW / 2;
  61. [timeBtn setTitleColor:[UIColor colorWithHexString:@"#333333"] forState:UIControlStateNormal];
  62. timeBtn.layer.masksToBounds = YES;
  63. timeBtn.imagePosition = QMUIButtonImagePositionRight;
  64. timeBtn.layer.cornerRadius = 4;
  65. [timeBtn setImage:[UIImage imageNamed:@"lr_live_go_up"] forState:UIControlStateNormal];
  66. timeBtn.spacingBetweenImageAndTitle = 20;
  67. [timeBtn addTarget:self action:@selector(timeBtnAction) forControlEvents:UIControlEventTouchUpInside];
  68. self.timeBtn = timeBtn;
  69. [self addSubview:timeBtn];
  70. UIButton *pkBtn = [[UIButton alloc]initWithFrame:CGRectMake(50, timeBtn.bottom + 40, kScreenW - 100, 44)];
  71. [pkBtn setTitle:ASLocalizedString(@"确定发起PK")
  72. forState:UIControlStateNormal];
  73. [pkBtn setBackgroundImage:[UIImage imageNamed:@"com_button_btn"] forState:UIControlStateNormal];
  74. [pkBtn setTitleColor:kWhiteColor forState:UIControlStateNormal];
  75. [pkBtn.titleLabel setFont:[UIFont systemFontOfSize:15]];
  76. [pkBtn addTarget:self action:@selector(pkBtnAction) forControlEvents:UIControlEventTouchUpInside];
  77. [self addSubview:pkBtn];
  78. self.backgroundColor = kWhiteColor;
  79. // [UIColor colorWithRed:0 green:0 blue:0 alpha:0.95];
  80. }
  81. break;
  82. case PKPopViewTypeDetail:
  83. {
  84. self.backgroundColor = kWhiteColor;
  85. UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, kScreenW, 44)];
  86. titleLabel.textColor = kBlackColor;
  87. titleLabel.textAlignment = NSTextAlignmentCenter;
  88. titleLabel.text = ASLocalizedString(@"约战PK")
  89. ;
  90. titleLabel.font = [UIFont systemFontOfSize:15];
  91. [self addSubview:titleLabel];
  92. UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(0, 43.5, kScreenW, 0.5)];
  93. lineView.backgroundColor = kBlackColor;
  94. [self addSubview:lineView];
  95. UIImageView *middleImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 100, 57, 12)];
  96. [middleImageView setImage:[UIImage imageNamed:@"bg_over_pk_middle"]];
  97. middleImageView.centerX = kScreenW / 2;
  98. [self addSubview:middleImageView];
  99. UIImageView *leftImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
  100. leftImageView.layer.cornerRadius = 50;
  101. leftImageView.clipsToBounds = YES;
  102. leftImageView.contentMode = UIViewContentModeScaleAspectFit;
  103. leftImageView.centerY = middleImageView.centerY;
  104. leftImageView.right = middleImageView.left - 10;
  105. [leftImageView sd_setImageWithURL:[NSURL URLWithString:[[[IMAPlatform sharedInstance] host] imUserIconUrl]] placeholderImage:kDefaultPreloadHeadImg];
  106. [self addSubview:leftImageView];
  107. self.leftImageView = leftImageView;
  108. UIImageView *rightImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
  109. rightImageView.layer.cornerRadius = 50;
  110. rightImageView.clipsToBounds = YES;
  111. rightImageView.contentMode = UIViewContentModeScaleAspectFit;
  112. rightImageView.centerY = middleImageView.centerY;
  113. rightImageView.left = middleImageView.right + 10;
  114. [self addSubview:rightImageView];
  115. self.rightImageView = rightImageView;
  116. UIButton *endPkBtn = [[UIButton alloc]initWithFrame:CGRectMake(64, self.rightImageView.bottom+20, kScreenW - 128, 44)];
  117. [endPkBtn setBackgroundImage:[UIImage imageNamed:@"bg_over_pk_text"] forState:UIControlStateNormal];
  118. [endPkBtn setTitle:ASLocalizedString(@"结束PK")
  119. forState:UIControlStateNormal];
  120. [endPkBtn.titleLabel setFont:[UIFont systemFontOfSize:15]];
  121. [endPkBtn setTitleColor:kWhiteColor forState:UIControlStateNormal];
  122. [endPkBtn addTarget:self action:@selector(endPkBtnAction) forControlEvents:UIControlEventTouchUpInside];
  123. [self addSubview:endPkBtn];
  124. }
  125. break;
  126. case PKPopViewSelectTime:
  127. {
  128. self.backgroundColor = kWhiteColor;
  129. [self addSubview:self.tableView];
  130. [self requestPKListData];
  131. }
  132. break;
  133. default:
  134. break;
  135. }
  136. }
  137. - (void)setPkTimeModel:(PKTimeModel *)pkTimeModel{
  138. _pkTimeModel = pkTimeModel;
  139. // self.timeLabel.text = [NSString stringWithFormat:ASLocalizedString(@"%@%@分钟"),kPKPopViewTimePrefix,pkTimeModel.time];
  140. [self.timeBtn setTitle:[NSString stringWithFormat:ASLocalizedString(@"%@%@分钟")
  141. ,kPKPopViewTimePrefix,pkTimeModel.time] forState:UIControlStateNormal];
  142. }
  143. #pragma mark - Data
  144. - (void)requestPKListData{
  145. NSMutableDictionary *dict = [[NSMutableDictionary alloc]init];
  146. // ctl=pk&act=pk_list
  147. if(![GlobalVariables sharedInstance].openAgora)
  148. {
  149. [dict setObject:@"pk_tencent" forKey:@"ctl"];
  150. }
  151. else
  152. {
  153. [dict setObject:@"pk_agora" forKey:@"ctl"];
  154. }
  155. [dict setValue:@"pk_list" forKey:@"act"];
  156. [self.httpsManager POSTWithParameters:dict SuccessBlock:^(NSDictionary *responseJson) {
  157. if ([responseJson toInt:@"status"] == 1) {
  158. NSLog(ASLocalizedString(@"PKList接口请求成功responseJson:%@")
  159. ,responseJson);
  160. [self.dataArray removeAllObjects];
  161. NSArray *listArray = [responseJson objectForKey:@"list"];
  162. if (listArray.count) {
  163. for (NSDictionary *dict in listArray) {
  164. PKTimeModel *model = [PKTimeModel mj_objectWithKeyValues:dict];
  165. [self.dataArray addObject:model];
  166. }
  167. }
  168. PKTimeModel *model = self.dataArray.firstObject;
  169. // if (self.timeLabel) {
  170. // self.timeLabel.text = [NSString stringWithFormat:ASLocalizedString(@"%@%@分钟"),kPKPopViewTimePrefix,model.time];
  171. // }
  172. if (self.timeBtn) {
  173. [self.timeBtn setTitle:[NSString stringWithFormat:ASLocalizedString(@"%@%@分钟")
  174. ,kPKPopViewTimePrefix,model.time] forState:UIControlStateNormal];
  175. }
  176. if (self.tableView) {
  177. [self.tableView reloadData];
  178. }
  179. }else{
  180. NSLog(ASLocalizedString(@"PKList接口请求失败responseJson:%@")
  181. ,responseJson);
  182. }
  183. } FailureBlock:^(NSError *error) {
  184. NSLog(ASLocalizedString(@"PKList接口请求失败error:%@")
  185. ,error);
  186. }];
  187. }
  188. - (void)requestPKUserInfo{
  189. NSMutableDictionary *dict = [[NSMutableDictionary alloc]init];
  190. [dict setValue:@"user" forKey:@"ctl"];
  191. [dict setValue:@"user_home" forKey:@"act"];
  192. [dict setValue:_otherId forKey:@"to_user_id"];
  193. [self.httpsManager POSTWithParameters:dict SuccessBlock:^(NSDictionary *responseJson) {
  194. if ([responseJson toInt:@"status"] == 1) {
  195. NSLog(ASLocalizedString(@"获取对方用户信息成功responseJson:%@")
  196. ,responseJson);
  197. [self.rightImageView sd_setImageWithURL:[NSURL URLWithString:responseJson[@"user"][@"head_image"]] placeholderImage:kDefaultPreloadHeadImg];
  198. }else{
  199. NSLog(ASLocalizedString(@"获取对方用户信息失败responseJson:%@")
  200. ,responseJson);
  201. }
  202. } FailureBlock:^(NSError *error) {
  203. NSLog(ASLocalizedString(@"获取对方用户信息失败error:%@")
  204. ,error);
  205. }];
  206. }
  207. #pragma mark - Action
  208. - (void)timeBtnAction{
  209. if (self.clickSetTimeBlcok) {
  210. self.clickSetTimeBlcok();
  211. }
  212. }
  213. - (void)pkBtnAction{
  214. [self hide];
  215. if (self.clickPkBtnBlock) {
  216. if (_pkTimeModel) {
  217. self.clickPkBtnBlock(_pkTimeModel.id);
  218. }else{
  219. PKTimeModel *model = self.dataArray.firstObject;
  220. self.clickPkBtnBlock(model.id);
  221. }
  222. }
  223. }
  224. - (void)endPkBtnAction{
  225. [self hide];
  226. if (self.clickEndPkBtnBlock) {
  227. self.clickEndPkBtnBlock();
  228. }
  229. }
  230. #pragma mark - UITableViewDelegate UITableViewDataSource
  231. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
  232. return self.dataArray.count;
  233. }
  234. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
  235. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([UITableViewCell class])];
  236. PKTimeModel *model = self.dataArray[indexPath.row];
  237. cell.textLabel.text = [NSString stringWithFormat:ASLocalizedString(@"%@分钟")
  238. ,model.time];
  239. return cell;
  240. }
  241. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
  242. return 49;
  243. }
  244. - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
  245. return 40;
  246. }
  247. - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
  248. UIView *headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenW, 40)];
  249. headerView.backgroundColor = kWhiteColor;
  250. UILabel *onlineListLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 0, kScreenW - 10, 40)];
  251. onlineListLabel.text = ASLocalizedString(@"PK时间选择")
  252. ;
  253. onlineListLabel.font = [UIFont systemFontOfSize:15];
  254. onlineListLabel.textColor = kBlackColor;
  255. UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(0, 39.5, kScreenW, 0.5)];
  256. lineView.backgroundColor = [kBlackColor colorWithAlphaComponent:0.3];
  257. [headerView addSubview:onlineListLabel];
  258. [headerView addSubview:lineView];
  259. return headerView;
  260. }
  261. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
  262. PKTimeModel *model = self.dataArray[indexPath.row];
  263. [self hide];
  264. if (self.clickTimeCellBlock) {
  265. self.clickTimeCellBlock(model);
  266. }
  267. }
  268. #pragma mark - Show And Hide
  269. - (void)show:(UIView *)superView{
  270. [superView addSubview:self.shadowView];
  271. [superView addSubview:self];
  272. // //2020-1-4 最上方
  273. // [superView bringSubviewToFront:self.shadowView];
  274. // //[self bringSubviewToFront:self.shadowView];
  275. if (_type == PKPopViewTypeDetail) {
  276. [self requestPKUserInfo];
  277. }
  278. [UIView animateWithDuration:0.25 animations:^{
  279. self.shadowView.alpha = 1;
  280. if (_type == PKPopViewTypeDetail) {
  281. self.y = kScreenH - 268;
  282. }else{
  283. self.y = kScreenH - 268;
  284. }
  285. }];
  286. }
  287. - (void)hide{
  288. [UIView animateWithDuration:0.25 animations:^{
  289. self.shadowView.alpha = 0;
  290. self.y = kScreenH;
  291. } completion:^(BOOL finished) {
  292. [self.shadowView removeFromSuperview];
  293. [self removeFromSuperview];
  294. }];
  295. }
  296. #pragma mark - Block
  297. - (void)setClickSetTimeBlcok:(clickSetTimeBlcok)clickSetTimeBlcok{
  298. _clickSetTimeBlcok = clickSetTimeBlcok;
  299. }
  300. - (void)setClickTimeCellBlock:(clickTimeCellBlock)clickTimeCellBlock{
  301. _clickTimeCellBlock = clickTimeCellBlock;
  302. }
  303. - (void)setClickPkBtnBlock:(clickPkBtnBlock)clickPkBtnBlock{
  304. _clickPkBtnBlock = clickPkBtnBlock;
  305. }
  306. - (void)setClickEndPkBtnBlock:(clickEndPkBtnBlock)clickEndPkBtnBlock{
  307. _clickEndPkBtnBlock = clickEndPkBtnBlock;
  308. }
  309. #pragma mark - Lazy Load
  310. - (NSMutableArray *)dataArray{
  311. if (!_dataArray) {
  312. _dataArray = [[NSMutableArray alloc]init];
  313. }
  314. return _dataArray;
  315. }
  316. - (UIView *)shadowView{
  317. if (!_shadowView) {
  318. _shadowView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenW, kScreenH)];
  319. _shadowView.backgroundColor = [kBlackColor colorWithAlphaComponent:0.5];
  320. _shadowView.alpha = 0;
  321. _shadowView.userInteractionEnabled = YES;
  322. UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(hide)];
  323. [_shadowView addGestureRecognizer:tap];
  324. }
  325. return _shadowView;
  326. }
  327. - (UITableView *)tableView{
  328. if (!_tableView) {
  329. _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, kScreenW, 200) style:UITableViewStylePlain];
  330. _tableView.delegate = self;
  331. _tableView.dataSource = self;
  332. [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:NSStringFromClass([UITableViewCell class])];
  333. }
  334. return _tableView;
  335. }
  336. @end