139 lines
5.7 KiB
Objective-C
139 lines
5.7 KiB
Objective-C
//
|
||
// QXDayTaskRuleView.m
|
||
// QXLive
|
||
//
|
||
// Created by 启星 on 2025/6/6.
|
||
//
|
||
|
||
#import "QXDayTaskRuleView.h"
|
||
#import <WebKit/WebKit.h>
|
||
static void *WKWebBrowserContext = &WKWebBrowserContext;
|
||
|
||
@interface QXDayTaskRuleView()<WKNavigationDelegate,WKUIDelegate>
|
||
@property(nonatomic,strong)WKWebView *contentWebView;
|
||
@property(nonatomic,strong)UIProgressView *progressView;
|
||
@property(nonatomic,strong)UIButton* closeBtn;
|
||
@end
|
||
|
||
@implementation QXDayTaskRuleView
|
||
|
||
- (instancetype)initWithFrame:(CGRect)frame
|
||
{
|
||
self = [super initWithFrame:frame];
|
||
if (self) {
|
||
[self initSubviews];
|
||
}
|
||
return self;
|
||
}
|
||
-(void)initSubviews{
|
||
self.backgroundColor = [UIColor colorWithWhite:0.0 alpha:0.3];
|
||
[self addSubview:self.contentWebView];
|
||
[self addSubview:self.progressView];
|
||
[self loadData];
|
||
|
||
self.closeBtn = [[UIButton alloc] initWithFrame:CGRectMake((self.width-30)/2, self.contentWebView.bottom+10, 30, 30)];
|
||
[self.closeBtn setBackgroundImage:[UIImage imageNamed:@"home_white_close"] forState:(UIControlStateNormal)];
|
||
[self.closeBtn addTarget:self action:@selector(closeAction) forControlEvents:(UIControlEventTouchUpInside)];
|
||
[self addSubview:self.closeBtn];
|
||
}
|
||
-(void)closeAction{
|
||
[self removeFromSuperview];
|
||
}
|
||
- (void)loadData {
|
||
NSString *urlStr = [NSString stringWithFormat:@"%@web/index.html#/pages/other/taskDesc",H5ServerUrl];
|
||
NSURL* url=[NSURL URLWithString:urlStr];
|
||
NSURLRequest *request =[NSURLRequest requestWithURL:url];
|
||
[self.contentWebView loadRequest:request];
|
||
}
|
||
|
||
|
||
//进度条
|
||
- (void)webViewWebContentProcessDidTerminate:(WKWebView *)webView {
|
||
}
|
||
|
||
//KVO监听进度条
|
||
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
|
||
|
||
if ([keyPath isEqualToString:NSStringFromSelector(@selector(estimatedProgress))] && object == self.contentWebView) {
|
||
[self.progressView setAlpha:1.0f];
|
||
BOOL animated = self.contentWebView.estimatedProgress > self.progressView.progress;
|
||
[self.progressView setProgress:self.contentWebView.estimatedProgress animated:animated];
|
||
|
||
// Once complete, fade out UIProgressView
|
||
if(self.contentWebView.estimatedProgress >= 1.0f) {
|
||
[UIView animateWithDuration:0.3f delay:0.3f options:UIViewAnimationOptionCurveEaseOut animations:^{
|
||
[self.progressView setAlpha:0.0f];
|
||
} completion:^(BOOL finished) {
|
||
[self.progressView setProgress:0.0f animated:NO];
|
||
}];
|
||
}
|
||
}
|
||
else {
|
||
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
|
||
}
|
||
}
|
||
|
||
#pragma mark - getters and setters
|
||
- (WKWebView *)contentWebView {
|
||
if (!_contentWebView) {
|
||
//设置网页的配置文件
|
||
WKWebViewConfiguration * configuration = [[WKWebViewConfiguration alloc]init];
|
||
// 允许可以与网页交互,选择视图
|
||
configuration.selectionGranularity = YES;
|
||
// web内容处理池pr
|
||
configuration.processPool = [[WKProcessPool alloc] init];
|
||
//自定义配置,一般用于 js调用oc方法(OC拦截URL中的数据做自定义操作)
|
||
WKUserContentController * UserContentController = [[WKUserContentController alloc]init];
|
||
// 是否支持记忆读取
|
||
configuration.suppressesIncrementalRendering = NO;
|
||
// 允许用户更改网页的设置
|
||
configuration.preferences.javaScriptEnabled = YES;
|
||
configuration.preferences.javaScriptCanOpenWindowsAutomatically = YES;
|
||
configuration.userContentController = UserContentController;
|
||
// 此处一定要做判断,因为是iOS9之后才有的方法,否则在iOS8下会崩溃
|
||
if ([[UIDevice currentDevice].systemVersion floatValue] >= 9.0) {
|
||
//允许视频播放
|
||
configuration.allowsAirPlayForMediaPlayback = YES;
|
||
// 允许在线播放
|
||
configuration.allowsInlineMediaPlayback = YES;
|
||
//开启手势触摸 默认设置就是NO。在ios8系统中会导致手势问题,程序崩溃
|
||
_contentWebView.allowsBackForwardNavigationGestures = YES;
|
||
}
|
||
_contentWebView = [[WKWebView alloc] initWithFrame:CGRectMake(25, NavContentHeight+40, self.width-25*2, self.height-(NavContentHeight+40)*2-30) configuration:configuration];
|
||
_contentWebView.backgroundColor = [UIColor clearColor];
|
||
[_contentWebView addRoundedCornersWithRadius:16];
|
||
_contentWebView.opaque = NO;
|
||
//适应你设定的尺寸
|
||
[_contentWebView sizeToFit];
|
||
_contentWebView.scrollView.showsVerticalScrollIndicator = NO;
|
||
_contentWebView.scrollView.backgroundColor = [UIColor clearColor];
|
||
_contentWebView.scrollView.bounces = NO;
|
||
// 设置代理
|
||
_contentWebView.navigationDelegate = self;
|
||
//kvo 添加进度监控
|
||
[_contentWebView addObserver:self forKeyPath:NSStringFromSelector(@selector(estimatedProgress)) options:0 context:WKWebBrowserContext];
|
||
}
|
||
return _contentWebView;
|
||
}
|
||
|
||
- (UIProgressView *)progressView {
|
||
if (!_progressView) {
|
||
_progressView = [[UIProgressView alloc]initWithProgressViewStyle:UIProgressViewStyleDefault];
|
||
_progressView.frame = CGRectMake(self.contentWebView.left+16, self.contentWebView.top, self.contentWebView.width-32, 2);
|
||
[_progressView setTrackTintColor:[UIColor colorWithRed:240.0/255 green:240.0/255 blue:240.0/255 alpha:1.0]];
|
||
_progressView.progressTintColor = QXConfig.themeColor;
|
||
}
|
||
return _progressView;
|
||
}
|
||
|
||
-(void)setProgressColor:(UIColor *)progressColor{
|
||
_progressView.progressTintColor = progressColor;
|
||
}
|
||
// 记得dealloc
|
||
- (void)dealloc {
|
||
if (self) {
|
||
[self.contentWebView removeObserver:self forKeyPath:NSStringFromSelector(@selector(estimatedProgress))];
|
||
}
|
||
}
|
||
@end
|