BogoGameListHeadView.m 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. //
  2. // BogoGameListHeadView.m
  3. // BuguLive
  4. //
  5. // Created by 宋晨光 on 2021/5/6.
  6. // Copyright © 2021 xfg. All rights reserved.
  7. //
  8. #import "BogoGameListHeadView.h"
  9. #import "BogoGameListHeadCell.h"
  10. #import "BGTopicTimeLineListController.h"
  11. #import "GameBottomListModel.h"
  12. #import "GamePopView.h"
  13. #import "NSString+Common.h"
  14. @implementation BogoGameListHeadView
  15. {
  16. }
  17. - (instancetype)initWithFrame:(CGRect)frame
  18. {
  19. self = [super initWithFrame:frame];
  20. if (self) {
  21. }
  22. return self;
  23. }
  24. - (void)awakeFromNib
  25. {
  26. [super awakeFromNib];
  27. // self.backgroundColor = [kBlackColor alpha];
  28. [self setUpView];
  29. [self requestGameList];
  30. }
  31. - (void)requestGameList{
  32. NSMutableDictionary *dict = [[NSMutableDictionary alloc]init];
  33. [dict setValue:@"app" forKey:@"ctl"];
  34. [dict setValue:@"room_game_list" forKey:@"act"];
  35. [[NetHttpsManager manager] POSTWithParameters:dict SuccessBlock:^(NSDictionary *responseJson) {
  36. if ([responseJson toInt:@"status"] == 1) {
  37. self.listArr = [NSArray modelArrayWithClass:GameBottomListModel.class json:responseJson[@"list"]];
  38. [self.collectionView reloadData];
  39. NSLog(@"游戏列表获取成功");
  40. }else{
  41. //接口请求失败
  42. }
  43. } FailureBlock:^(NSError *error) {
  44. NSLog(ASLocalizedString(@"游戏列表请求数据失败error:%@"),error);
  45. }];
  46. }
  47. -(void)setUpView{
  48. [self.collectionView registerNib:[UINib nibWithNibName:@"BogoGameListHeadCell" bundle:nil] forCellWithReuseIdentifier:@"BogoGameListHeadCell"];
  49. self.collectionView.backgroundColor = [UIColor clearColor];
  50. self.collectionView.delegate = self;
  51. self.collectionView.dataSource = self;
  52. }
  53. -(void)resetTopicModel:(NSArray *)arr{
  54. self.listArr = [NSMutableArray arrayWithArray:arr];
  55. [self.collectionView reloadData];
  56. // for (int i = 0; i < 4; i ++) {
  57. // if (i > 4) return;
  58. // MGNewDTHeadControl *control = [self.topicView viewWithTag:100 + i];
  59. //
  60. // if(arr.count > i)
  61. // {
  62. // [control resetControlModel:arr[i]];
  63. // //给有数据的view添加手势
  64. // [control addTarget:self action:@selector(MGNewDTHeadAction:) forControlEvents:UIControlEventTouchUpInside];
  65. // }
  66. // }
  67. }
  68. - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
  69. return 1;
  70. }
  71. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
  72. return self.listArr.count;
  73. }
  74. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
  75. BogoGameListHeadCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"BogoGameListHeadCell" forIndexPath:indexPath];
  76. // cell.backgroundColor = kBlueColor;
  77. [cell resetControlModel:self.listArr[indexPath.row]];
  78. return cell;
  79. }
  80. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
  81. // BGTopicTimeLineListController *pushVC = [BGTopicTimeLineListController new];
  82. GameBottomListModel *model = self.listArr[indexPath.row];
  83. // GamePopView *pop = [[GamePopView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
  84. // [pop setURL:model.url];
  85. //// [pop show:[self getTopMostController].view];
  86. // [pop show:self.superview];
  87. self.webView.frame = self.superview.superview.superview.bounds;
  88. // self.webView.navigationDelegate = self; // 重点:设置navigationDelegate
  89. NSString *url = model.url;
  90. url = [url urlAddCompnentForValue:[IMAPlatform sharedInstance].host.userId key:@"uid"];
  91. url = [url urlAddCompnentForValue:[BogoNetwork shareInstance].token key:@"token"];
  92. [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:url]]];
  93. [self.superview.superview.superview addSubview:self.webView];
  94. }
  95. - (WKWebView *)webView{
  96. if (!_webView) {
  97. WKWebViewConfiguration *config = [[WKWebViewConfiguration alloc]init];
  98. // config.backgroundColor = [UIColor clearColor];
  99. // config.userContentController.backgroundColor = [UIColor clearColor];
  100. _webView = [[WKWebView alloc]initWithFrame:CGRectZero configuration:config];
  101. _webView.opaque = NO;
  102. _webView.navigationDelegate = self;
  103. _webView.scrollView.backgroundColor = [UIColor clearColor];
  104. [_webView setBackgroundColor:[UIColor clearColor]];
  105. UIButton *closeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  106. closeBtn.frame = CGRectMake(40, kStatusBarHeight, 30, 30);
  107. [closeBtn setImage:[UIImage imageNamed:@"com_close_1"] forState:UIControlStateNormal];
  108. [closeBtn addTarget:self action:@selector(clickCloseBtn) forControlEvents:UIControlEventTouchUpInside];
  109. [_webView addSubview:closeBtn];
  110. NSDictionary *dict = [[NSUserDefaults standardUserDefaults] objectForKey:@"BogoNetworkIndexModel"];
  111. BogoNetworkInitModel *model = [BogoNetworkInitModel mj_objectWithKeyValues:dict];
  112. }
  113. return _webView;
  114. }
  115. - (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction
  116. decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler {
  117. NSURL *url = navigationAction.request.URL;
  118. // 判断是否是您希望拦截的 URL,这里的示例是以 "myapp://" 开头的 URL
  119. if ([url.absoluteString isEqualToString:@"bogogame://exit"]) {
  120. [self clickCloseBtn];
  121. // 自己处理逻辑,不加载该 URL
  122. decisionHandler(WKNavigationActionPolicyCancel);
  123. } else {
  124. // 允许加载该 URL
  125. decisionHandler(WKNavigationActionPolicyAllow);
  126. }
  127. }
  128. - (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation {
  129. // 判断URL是否为"bogogame://exit",如果是,则隐藏webView
  130. if ([webView.URL.absoluteString isEqualToString:@"bogogame://exit"]) {
  131. [self clickCloseBtn];
  132. }
  133. }
  134. - (void)clickCloseBtn {
  135. [_webView removeFromSuperview];
  136. }
  137. //获取当前最上层的控制器
  138. - (UIViewController *)getTopMostController {
  139. UIViewController *topVC = [UIApplication sharedApplication].keyWindow.rootViewController;
  140. //循环之前tempVC和topVC是一样的
  141. UIViewController *tempVC = topVC;
  142. while (1) {
  143. if ([topVC isKindOfClass:[UITabBarController class]]) {
  144. topVC = ((UITabBarController*)topVC).selectedViewController;
  145. }
  146. if ([topVC isKindOfClass:[UINavigationController class]]) {
  147. topVC = ((UINavigationController*)topVC).visibleViewController;
  148. }
  149. if (topVC.presentedViewController) {
  150. topVC = topVC.presentedViewController;
  151. }
  152. //如果两者一样,说明循环结束了
  153. if ([tempVC isEqual:topVC]) {
  154. break;
  155. } else {
  156. //如果两者不一样,继续循环
  157. tempVC = topVC;
  158. }
  159. }
  160. return topVC;
  161. }
  162. //- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
  163. //{
  164. //
  165. // return CGSizeMake((kScreenW-30)/2.0f, (kScreenW-30) / 2.0f + kRealValue(30));
  166. //}
  167. //
  168. //- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
  169. //
  170. // return 10;
  171. //}
  172. //
  173. //- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
  174. //
  175. // return 0;
  176. //}
  177. //
  178. //- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
  179. //{
  180. // return UIEdgeInsetsMake(10, 10, 0, 10);
  181. //}
  182. @end