| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184 |
- //
- // WardTipView.m
- // BuguLive
- //
- // Created by 范东 on 2019/2/11.
- // Copyright © 2019 xfg. All rights reserved.
- //
- #import "GamePopView.h"
- @interface GamePopView()<WKUIDelegate,WKNavigationDelegate>
- @property (nonatomic, strong) WKWebView *webView;
- @property (nonatomic, strong) UIView *shadowView;
- @property (nonatomic, copy) tipWebViewDidFinishLoadBlock tipWebViewDidFinishLoadBlock;
- @end
- @implementation GamePopView
- - (instancetype)initWithFrame:(CGRect)frame{
- if (self = [super initWithFrame:frame]) {
- // self.alpha = 1;
- self.layer.shadowColor = kBlackColor.CGColor;
- self.layer.shadowOffset = CGSizeMake(2, 5);
- self.layer.shadowOpacity = 0.5;
- [self initSubview];
-
-
- }
- return self;
- }
- - (void)initSubview{
- [self addSubview:self.webView];
- }
- #pragma mark - WkWebViewDelegate
- - (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation {
- // [webView evaluateJavaScript:@"document.body.offsetHeight" completionHandler:^(id _Nullable result, NSError * _Nullable error) {
- // CGFloat documentHeight = [result doubleValue];
- // CGRect webFrame = webView.frame;
- // webFrame.size.height = documentHeight;
- // webView.frame = webFrame;
- // self.height = documentHeight;
- // }];
- }
- - (void)show:(UIView *)superView{
- [superView addSubview:self.shadowView];
- [superView addSubview:self];
-
- }
- - (void)hide{
- [UIView animateWithDuration:0.25 animations:^{
- self.alpha = 0;
- self.frame = CGRectMake(80, kScreenH, kScreenW - 160, kScreenH / 2);
- } completion:^(BOOL finished) {
- [self.shadowView removeFromSuperview];
- [self removeFromSuperview];
- }];
- }
- - (void)setURL:(NSString *)url{
- [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@&token=%@",url,[GlobalVariables sharedInstance].token]]]];
- }
- - (void)setTipWebViewDidFinishLoadBlock:(tipWebViewDidFinishLoadBlock)tipWebViewDidFinishLoadBlock{
- _tipWebViewDidFinishLoadBlock = tipWebViewDidFinishLoadBlock;
- }
- - (WKWebView *)webView{
- if (!_webView) {
- WKWebViewConfiguration *config = [[WKWebViewConfiguration alloc]init];
- _webView = [[WKWebView alloc]initWithFrame:self.bounds configuration:config];
- _webView.UIDelegate = self;
- _webView.navigationDelegate = self;
- _webView.scrollView.showsVerticalScrollIndicator = NO;
- _webView.scrollView.showsHorizontalScrollIndicator = NO;
- _webView.backgroundColor = [UIColor clearColor];
- _webView.opaque = NO;
- }
- return _webView;
- }
- #pragma mark - WKNavigationDelegate
- - (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler
- {
- NSURL *URL = navigationAction.request.URL;
- NSString *scheme = [URL scheme];
- if ([scheme isEqualToString:@"live"]) {
- [self handleCustomAction:URL];
- decisionHandler(WKNavigationActionPolicyCancel);
- return;
- }
- decisionHandler(WKNavigationActionPolicyAllow);
- }
- /**
- url参数转字典
-
- @param urlStr 输入的url
- @return 转换好的字典
- */
- -(NSDictionary *)dictionaryWithUrlString:(NSString *)urlStr
- {
-
- NSMutableDictionary *parm = [[NSMutableDictionary alloc]init];
-
- //传入url创建url组件类
- NSURLComponents *urlComponents = [[NSURLComponents alloc] initWithString:urlStr];
-
- //回调遍历所有参数,添加入字典
- [urlComponents.queryItems enumerateObjectsUsingBlock:^(NSURLQueryItem * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
- [parm setObject:obj.value forKey:obj.name];
- }];
-
- return parm;
-
- // if (urlStr && urlStr.length && [urlStr rangeOfString:@"?"].length == 1) {
- // NSArray *array = [urlStr componentsSeparatedByString:@"?"];
- // if (array && array.count == 2) {
- // NSString *paramsStr = array[1];
- // if (paramsStr.length) {
- // NSMutableDictionary *paramsDict = [NSMutableDictionary dictionary];
- // NSArray *paramArray = [paramsStr componentsSeparatedByString:@"&"];
- // for (NSString *param in paramArray) {
- // if (param && param.length) {
- // NSArray *parArr = [param componentsSeparatedByString:@"="];
- // if (parArr.count == 2) {
- // [paramsDict setObject:parArr[1] forKey:parArr[0]];
- // }
- // }
- // }
- // return paramsDict;
- // }else{
- // return nil;
- // }
- // }else{
- // return nil;
- // }
- // }else{
- // return nil;
- // }
- }
- #pragma mark - private method
- - (void)handleCustomAction:(NSURL *)URL
- {
- NSString *host = [URL host];
-
- NSDictionary *queryDic = [self dictionaryWithUrlString:URL.absoluteString];
- if ([queryDic[@"action"] isEqualToString:@"close"]) {
- [self hide];
- }
- }
- - (UIView *)shadowView{
- if (!_shadowView) {
- _shadowView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenW, kScreenH)];
- _shadowView.backgroundColor = kClearColor;
- _shadowView.userInteractionEnabled = YES;
- UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(hide)];
- [_shadowView addGestureRecognizer:tap];
- }
- return _shadowView;
- }
- /*
- // Only override drawRect: if you perform custom drawing.
- // An empty implementation adversely affects performance during animation.
- - (void)drawRect:(CGRect)rect {
- // Drawing code
- }
- */
- @end
|