|
| 1 | +// |
| 2 | +// SLWebTableViewController.m |
| 3 | +// DarkMode |
| 4 | +// |
| 5 | +// Created by wsl on 2020/5/22. |
| 6 | +// Copyright © 2020 https://github.com/wsl2ls ----- . All rights reserved. |
| 7 | +// |
| 8 | + |
| 9 | +#import "SLWebTableViewController.h" |
| 10 | +#import <WebKit/WebKit.h> |
| 11 | + |
| 12 | +@interface SLWebTableViewController ()<UITableViewDelegate,UITableViewDataSource> |
| 13 | + |
| 14 | +@property (nonatomic, strong) WKWebView * webView; |
| 15 | +///网页加载进度视图 |
| 16 | +@property (nonatomic, strong) UIProgressView * progressView; |
| 17 | +/// WKWebView 内容的高度 |
| 18 | +@property (nonatomic, assign) CGFloat webContentHeight; |
| 19 | + |
| 20 | +@property (nonatomic, strong) UITableView *tableView; |
| 21 | + |
| 22 | +@end |
| 23 | + |
| 24 | +@implementation SLWebTableViewController |
| 25 | + |
| 26 | +- (void)viewDidLoad { |
| 27 | + [super viewDidLoad]; |
| 28 | + [self setupUi]; |
| 29 | + [self addKVO]; |
| 30 | +} |
| 31 | +- (void)viewWillDisappear:(BOOL)animated { |
| 32 | + [super viewWillDisappear:animated]; |
| 33 | + [self.progressView removeFromSuperview]; |
| 34 | +} |
| 35 | +- (void)dealloc { |
| 36 | + [self removeKVO]; |
| 37 | + NSLog(@"%@释放了",NSStringFromClass(self.class)); |
| 38 | +} |
| 39 | + |
| 40 | +#pragma mark - SetupUI |
| 41 | +- (void)setupUi { |
| 42 | + self.view.backgroundColor = [UIColor whiteColor]; |
| 43 | + [self.view addSubview:self.tableView]; |
| 44 | + [self configureWebTable]; |
| 45 | +} |
| 46 | +- (void)configureWebTable { |
| 47 | + self.tableView.tableHeaderView = self.webView; |
| 48 | + self.tableView.bounces = NO; |
| 49 | + self.webView.scrollView.bounces = NO; |
| 50 | +} |
| 51 | + |
| 52 | +#pragma mark - Getter |
| 53 | +- (UITableView *)tableView { |
| 54 | + if (_tableView == nil) { |
| 55 | + _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SL_kScreenWidth, SL_kScreenHeight) style:UITableViewStyleGrouped]; |
| 56 | + _tableView.delegate = self; |
| 57 | + _tableView.dataSource = self; |
| 58 | + _tableView.estimatedRowHeight = 0; |
| 59 | + [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cellId"]; |
| 60 | + } |
| 61 | + return _tableView; |
| 62 | +} |
| 63 | +- (WKWebView *)webView { |
| 64 | + if(_webView == nil){ |
| 65 | + //创建网页配置 |
| 66 | + WKWebViewConfiguration *config = [[WKWebViewConfiguration alloc] init]; |
| 67 | + _webView = [[WKWebView alloc] initWithFrame:CGRectMake(0, 0, SL_kScreenWidth, SL_kScreenHeight) configuration:config]; |
| 68 | + NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"https://www.jianshu.com/p/5cf0d241ae12"]]; |
| 69 | + [_webView loadRequest:request]; |
| 70 | + } |
| 71 | + return _webView; |
| 72 | +} |
| 73 | +- (UIProgressView *)progressView { |
| 74 | + if (!_progressView){ |
| 75 | + _progressView = [[UIProgressView alloc] initWithFrame:CGRectMake(0, 0, SL_kScreenWidth, 2)]; |
| 76 | + _progressView.tintColor = [UIColor blueColor]; |
| 77 | + _progressView.trackTintColor = [UIColor clearColor]; |
| 78 | + } |
| 79 | + if (_progressView.superview == nil) { |
| 80 | + [self.navigationController.navigationBar addSubview:_progressView]; |
| 81 | + } |
| 82 | + return _progressView; |
| 83 | +} |
| 84 | + |
| 85 | +#pragma mark - KVO |
| 86 | +///添加键值对监听 |
| 87 | +- (void)addKVO { |
| 88 | + //监听网页加载进度 |
| 89 | + [self.webView addObserver:self |
| 90 | + forKeyPath:NSStringFromSelector(@selector(estimatedProgress)) |
| 91 | + options:NSKeyValueObservingOptionNew |
| 92 | + context:nil]; |
| 93 | + //监听网页内容高度 |
| 94 | + [self.webView.scrollView addObserver:self |
| 95 | + forKeyPath:@"contentSize" |
| 96 | + options:NSKeyValueObservingOptionNew |
| 97 | + context:nil]; |
| 98 | +} |
| 99 | +///移除监听 |
| 100 | +- (void)removeKVO { |
| 101 | + //移除观察者 |
| 102 | + [_webView removeObserver:self |
| 103 | + forKeyPath:NSStringFromSelector(@selector(estimatedProgress))]; |
| 104 | + [_webView.scrollView removeObserver:self |
| 105 | + forKeyPath:NSStringFromSelector(@selector(contentSize))]; |
| 106 | +} |
| 107 | +//kvo监听 必须实现此方法 |
| 108 | +-(void)observeValueForKeyPath:(NSString *)keyPath |
| 109 | + ofObject:(id)object |
| 110 | + change:(NSDictionary<NSKeyValueChangeKey,id> *)change |
| 111 | + context:(void *)context{ |
| 112 | + |
| 113 | + if ([keyPath isEqualToString:NSStringFromSelector(@selector(estimatedProgress))] |
| 114 | + && object == _webView) { |
| 115 | + // NSLog(@"网页加载进度 = %f",_webView.estimatedProgress); |
| 116 | + self.progressView.progress = _webView.estimatedProgress; |
| 117 | + if (_webView.estimatedProgress >= 1.0f) { |
| 118 | + dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ |
| 119 | + self.progressView.progress = 0; |
| 120 | + }); |
| 121 | + } |
| 122 | + }else if ([keyPath isEqualToString:NSStringFromSelector(@selector(contentSize))] |
| 123 | + && object == _webView.scrollView && _webContentHeight != _webView.scrollView.contentSize.height) { |
| 124 | + _webContentHeight = _webView.scrollView.contentSize.height; |
| 125 | + NSLog(@"WebViewContentSize = %@",NSStringFromCGSize(_webView.scrollView.contentSize)) |
| 126 | + } |
| 127 | +} |
| 128 | + |
| 129 | +#pragma mark - EventsHandle |
| 130 | + |
| 131 | +#pragma mark - HelpMethods |
| 132 | + |
| 133 | +#pragma mark - UITableViewDelegate,UITableViewDataSource |
| 134 | +- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { |
| 135 | + return 1; |
| 136 | +} |
| 137 | +- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { |
| 138 | + return 20; |
| 139 | +} |
| 140 | +- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { |
| 141 | + return 80; |
| 142 | +} |
| 143 | +- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section { |
| 144 | + return 44; |
| 145 | +} |
| 146 | +- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { |
| 147 | + UILabel *label = [UILabel new]; |
| 148 | + label.text = @"评论"; |
| 149 | + label.textColor = UIColor.whiteColor; |
| 150 | + label.textAlignment = NSTextAlignmentCenter; |
| 151 | + label.backgroundColor = [UIColor orangeColor]; |
| 152 | + return label; |
| 153 | +} |
| 154 | +- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section { |
| 155 | + return 0.1; |
| 156 | +} |
| 157 | +- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section { |
| 158 | + return nil; |
| 159 | +} |
| 160 | +- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { |
| 161 | + UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cellId" forIndexPath:indexPath]; |
| 162 | + cell.textLabel.text = [NSString stringWithFormat:@"第%ld条评论",(long)indexPath.row]; |
| 163 | + return cell; |
| 164 | +} |
| 165 | + |
| 166 | +@end |
0 commit comments