GamePopView.m 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. //
  2. // WardTipView.m
  3. // BuguLive
  4. //
  5. // Created by 范东 on 2019/2/11.
  6. // Copyright © 2019 xfg. All rights reserved.
  7. //
  8. #import "GamePopView.h"
  9. @interface GamePopView()<WKUIDelegate,WKNavigationDelegate>
  10. @property (nonatomic, strong) WKWebView *webView;
  11. @property (nonatomic, strong) UIView *shadowView;
  12. @property (nonatomic, copy) tipWebViewDidFinishLoadBlock tipWebViewDidFinishLoadBlock;
  13. @end
  14. @implementation GamePopView
  15. - (instancetype)initWithFrame:(CGRect)frame{
  16. if (self = [super initWithFrame:frame]) {
  17. // self.alpha = 1;
  18. self.layer.shadowColor = kBlackColor.CGColor;
  19. self.layer.shadowOffset = CGSizeMake(2, 5);
  20. self.layer.shadowOpacity = 0.5;
  21. [self initSubview];
  22. }
  23. return self;
  24. }
  25. - (void)initSubview{
  26. [self addSubview:self.webView];
  27. }
  28. #pragma mark - WkWebViewDelegate
  29. - (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation {
  30. // [webView evaluateJavaScript:@"document.body.offsetHeight" completionHandler:^(id _Nullable result, NSError * _Nullable error) {
  31. // CGFloat documentHeight = [result doubleValue];
  32. // CGRect webFrame = webView.frame;
  33. // webFrame.size.height = documentHeight;
  34. // webView.frame = webFrame;
  35. // self.height = documentHeight;
  36. // }];
  37. }
  38. - (void)show:(UIView *)superView{
  39. [superView addSubview:self.shadowView];
  40. [superView addSubview:self];
  41. }
  42. - (void)hide{
  43. [UIView animateWithDuration:0.25 animations:^{
  44. self.alpha = 0;
  45. self.frame = CGRectMake(80, kScreenH, kScreenW - 160, kScreenH / 2);
  46. } completion:^(BOOL finished) {
  47. [self.shadowView removeFromSuperview];
  48. [self removeFromSuperview];
  49. }];
  50. }
  51. - (void)setURL:(NSString *)url{
  52. [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@&token=%@",url,[GlobalVariables sharedInstance].token]]]];
  53. }
  54. - (void)setTipWebViewDidFinishLoadBlock:(tipWebViewDidFinishLoadBlock)tipWebViewDidFinishLoadBlock{
  55. _tipWebViewDidFinishLoadBlock = tipWebViewDidFinishLoadBlock;
  56. }
  57. - (WKWebView *)webView{
  58. if (!_webView) {
  59. WKWebViewConfiguration *config = [[WKWebViewConfiguration alloc]init];
  60. _webView = [[WKWebView alloc]initWithFrame:self.bounds configuration:config];
  61. _webView.UIDelegate = self;
  62. _webView.navigationDelegate = self;
  63. _webView.scrollView.showsVerticalScrollIndicator = NO;
  64. _webView.scrollView.showsHorizontalScrollIndicator = NO;
  65. _webView.backgroundColor = [UIColor clearColor];
  66. _webView.opaque = NO;
  67. }
  68. return _webView;
  69. }
  70. #pragma mark - WKNavigationDelegate
  71. - (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler
  72. {
  73. NSURL *URL = navigationAction.request.URL;
  74. NSString *scheme = [URL scheme];
  75. if ([scheme isEqualToString:@"live"]) {
  76. [self handleCustomAction:URL];
  77. decisionHandler(WKNavigationActionPolicyCancel);
  78. return;
  79. }
  80. decisionHandler(WKNavigationActionPolicyAllow);
  81. }
  82. /**
  83. url参数转字典
  84. @param urlStr 输入的url
  85. @return 转换好的字典
  86. */
  87. -(NSDictionary *)dictionaryWithUrlString:(NSString *)urlStr
  88. {
  89. NSMutableDictionary *parm = [[NSMutableDictionary alloc]init];
  90. //传入url创建url组件类
  91. NSURLComponents *urlComponents = [[NSURLComponents alloc] initWithString:urlStr];
  92. //回调遍历所有参数,添加入字典
  93. [urlComponents.queryItems enumerateObjectsUsingBlock:^(NSURLQueryItem * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
  94. [parm setObject:obj.value forKey:obj.name];
  95. }];
  96. return parm;
  97. // if (urlStr && urlStr.length && [urlStr rangeOfString:@"?"].length == 1) {
  98. // NSArray *array = [urlStr componentsSeparatedByString:@"?"];
  99. // if (array && array.count == 2) {
  100. // NSString *paramsStr = array[1];
  101. // if (paramsStr.length) {
  102. // NSMutableDictionary *paramsDict = [NSMutableDictionary dictionary];
  103. // NSArray *paramArray = [paramsStr componentsSeparatedByString:@"&"];
  104. // for (NSString *param in paramArray) {
  105. // if (param && param.length) {
  106. // NSArray *parArr = [param componentsSeparatedByString:@"="];
  107. // if (parArr.count == 2) {
  108. // [paramsDict setObject:parArr[1] forKey:parArr[0]];
  109. // }
  110. // }
  111. // }
  112. // return paramsDict;
  113. // }else{
  114. // return nil;
  115. // }
  116. // }else{
  117. // return nil;
  118. // }
  119. // }else{
  120. // return nil;
  121. // }
  122. }
  123. #pragma mark - private method
  124. - (void)handleCustomAction:(NSURL *)URL
  125. {
  126. NSString *host = [URL host];
  127. NSDictionary *queryDic = [self dictionaryWithUrlString:URL.absoluteString];
  128. if ([queryDic[@"action"] isEqualToString:@"close"]) {
  129. [self hide];
  130. }
  131. }
  132. - (UIView *)shadowView{
  133. if (!_shadowView) {
  134. _shadowView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenW, kScreenH)];
  135. _shadowView.backgroundColor = kClearColor;
  136. _shadowView.userInteractionEnabled = YES;
  137. UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(hide)];
  138. [_shadowView addGestureRecognizer:tap];
  139. }
  140. return _shadowView;
  141. }
  142. /*
  143. // Only override drawRect: if you perform custom drawing.
  144. // An empty implementation adversely affects performance during animation.
  145. - (void)drawRect:(CGRect)rect {
  146. // Drawing code
  147. }
  148. */
  149. @end