// // FKProServeDetailView.m // FirstLink // // Created by jack on 16/8/16. // Copyright © 2016年 FirstLink. All rights reserved. // #import "FKProServeDetailView.h" #import "FKTargetConfigUtil.h" #import "WebViewController.h" #import "FLControllerHelper.h" #import @interface FKProServeDetailView () @property (nonatomic, strong) UIView *contentView; @property (nonatomic, strong) UILabel *titleLabel; @property (nonatomic, strong) UIButton *cancelBtn; @property (nonatomic, strong) WKWebView *webview; @end @implementation FKProServeDetailView - (instancetype)initWithFrame:(CGRect)frame{ if (self = [super initWithFrame:frame]) { self.backgroundColor = [UIColor whiteColor]; [self addAllSubviews]; [self addTapGesture]; } return self; } - (void)requestWithURL:(NSURL *)url { if (url) { NSURLRequest *request = [NSURLRequest requestWithURL:url]; [self.webview loadRequest:request]; } } - (void)showInView:(UIView *)view animated:(BOOL)animated{ if (!view) { return; } CGFloat height = view.frame.size.height; CGFloat width = view.frame.size.width; CGFloat contentH = 304; // 内容高度 [view addSubview:self]; [self setFrame:view.frame]; [self.contentView setFrame:CGRectMake(0, height, width, contentH)]; self.backgroundColor = [UIColor clearColor]; CGFloat duration = animated ? 0.3f : 0; WeakSelf(weakSelf); [UIView animateWithDuration:duration delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{ weakSelf.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5f]; [weakSelf.contentView setFrame:CGRectMake(0, height - contentH, width, contentH)]; } completion:nil]; } - (void)dismissAnimated{ if (self.superview){ [UIView animateWithDuration:0.3f delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{ self.backgroundColor = [UIColor clearColor]; [self.contentView setFrame:CGRectOffset(self.contentView.frame, 0, CGRectGetHeight(self.contentView.frame))]; } completion:^(BOOL finished) { [self removeFromSuperview]; }]; } } - (void)clickCancelBtn{ [self dismissAnimated]; } #pragma mark - - (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler { NSURLRequest *request = navigationAction.request; NSRange range = [request.URL.absoluteString rangeOfString:@"new_webview" options:NSCaseInsensitiveSearch]; if (range.length > 0) { decisionHandler(WKNavigationActionPolicyCancel); WebViewController *controller = [[WebViewController alloc] init]; controller.url = request.URL.absoluteString; controller.hidesBottomBarWhenPushed = YES; [[FLControllerHelper currentNavigationController] pushViewController:controller animated:YES]; } else { decisionHandler(WKNavigationActionPolicyAllow); } } #pragma mark - Gesture - (void)addTapGesture{ UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapGestureClick:)]; [self addGestureRecognizer:tap]; } - (void)tapGestureClick:(UIGestureRecognizer *)tapGesture{ CGPoint location = [tapGesture locationInView:self]; BOOL isOnContent = CGRectContainsPoint(self.contentView.frame, location); if (isOnContent) { return; } else { [self clickCancelBtn]; } } #pragma mark - Layout - (void)addAllSubviews{ [self addSubview:self.contentView]; [self.contentView addSubview:self.titleLabel]; [self.contentView addSubview:self.cancelBtn]; [self.contentView addSubview:self.webview]; [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(self.contentView).offset(15); make.centerY.equalTo(self.contentView.mas_top).offset(22); }]; [self.cancelBtn mas_makeConstraints:^(MASConstraintMaker *make) { make.top.right.equalTo(self.contentView); make.size.mas_equalTo(CGSizeMake(45, 44)); }]; [self.webview mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(self.contentView).offset(44); make.left.right.bottom.equalTo(self.contentView); }]; } #pragma mark - Property - (WKWebView *)webview { if (!_webview) { _webview = [[WKWebView alloc] init]; _webview.navigationDelegate = self; _webview.scrollView.showsHorizontalScrollIndicator = NO; _webview.backgroundColor = [UIColor whiteColor]; } return _webview; } - (UILabel *)titleLabel{ if (_titleLabel == nil) { _titleLabel = [[UILabel alloc]init]; _titleLabel.font = [UIFont systemFontOfSize:14]; _titleLabel.textColor = UIColorFromRGB(0x999999); _titleLabel.text = @"服务说明"; } return _titleLabel; } - (UIButton *)cancelBtn{ if (_cancelBtn == nil) { _cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom]; [_cancelBtn setImage:[UIImage imageNamed:@"shareCancel"] forState:UIControlStateNormal]; [_cancelBtn addTarget:self action:@selector(clickCancelBtn) forControlEvents:UIControlEventTouchUpInside]; } return _cancelBtn; } - (UIView *)contentView{ if (_contentView == nil) { _contentView = [[UIView alloc]init]; _contentView.backgroundColor = [UIColor whiteColor]; } return _contentView; } @end