省钱达人

DRWebDetailController.m 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. //
  2. // DRWebDetailController.m
  3. // YouHuiProject
  4. //
  5. // Created by 小花 on 2018/5/29.
  6. // Copyright © 2018年 kuxuan. All rights reserved.
  7. //
  8. #import "DRWebDetailController.h"
  9. #import <WebKit/WebKit.h>
  10. @interface DRWebDetailController ()<WKUIDelegate, WKNavigationDelegate>
  11. @property (nonatomic, strong) WKWebView *webView;
  12. /** 加载进度条 */
  13. @property (nonatomic, strong) UIProgressView *progressView;
  14. @property (nonatomic, strong) UIButton *backButton;
  15. @property (nonatomic, strong) UIButton *closeButton;
  16. @end
  17. @implementation DRWebDetailController
  18. - (void)viewDidLoad {
  19. [super viewDidLoad];
  20. [self configNavigationBar];
  21. [self loadRequest];
  22. }
  23. - (void)configNavigationBar {
  24. self.view.backgroundColor = [UIColor whiteColor];
  25. [self.view addSubview:self.webView];
  26. [self.view addSubview:self.progressView];
  27. self.navigationBar.showNavigationBarBottomLine = YES;
  28. [self.navigationBar setNavTitle:@"商品详情"];
  29. [self.navigationBar setCustomLeftButtons:@[self.backButton]];
  30. self.navigationBar.backgroundColor = [UIColor clearColor];
  31. if (@available(iOS 11.0, *)) {
  32. self.webView.scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;//UIScrollView也适用
  33. }else {
  34. self.automaticallyAdjustsScrollViewInsets = NO;
  35. }
  36. }
  37. - (void)loadRequest {
  38. NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:self.url]];
  39. [self.webView loadRequest:request];
  40. }
  41. /**
  42. kvo监听进度条
  43. @param keyPath keyPath description
  44. @param object object description
  45. @param change change description
  46. @param context context description
  47. */
  48. - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context{
  49. if ([keyPath isEqualToString:NSStringFromSelector(@selector(estimatedProgress))] && object == self.webView ) {
  50. [self.progressView setAlpha:1.0];
  51. [self.progressView setProgress:self.webView.estimatedProgress ];
  52. if (self.webView.estimatedProgress >= 1.0) {
  53. [UIView animateWithDuration:0.7 animations:^{
  54. [self.progressView setProgress:1.0 animated:YES];
  55. [self.progressView setAlpha:0.0];
  56. }];
  57. }
  58. }else{
  59. [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
  60. }
  61. }
  62. #pragma mark-
  63. #pragma mark- WKNavigationDelegate delegate
  64. /**
  65. 开始加载web的时候
  66. @param webView webView description
  67. @param navigation navigation description
  68. */
  69. - (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(WKNavigation *)navigation{
  70. //开始加载的时候,让进度条显示
  71. self.progressView.hidden = NO;
  72. self.progressView.progress = 0;
  73. self.progressView.alpha = 1.0;
  74. [UIView animateWithDuration:0.8 animations:^{
  75. self.progressView.progress = 0.6;
  76. }];
  77. }
  78. /**
  79. 当网页加载完成的时候调用
  80. @param webView web描述
  81. @param navigation 导航的描述
  82. */
  83. - (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation{
  84. self.title = self.webView.title;
  85. [[UIApplication sharedApplication]setNetworkActivityIndicatorVisible:NO];
  86. [self updateNavigationBarButtons];
  87. }
  88. #pragma mark -----------
  89. - (void)updateNavigationBarButtons {
  90. if (self.webView.canGoBack) {
  91. [self.navigationBar setCustomLeftButtons:@[self.backButton,self.closeButton]];
  92. }else {
  93. [self.navigationBar setCustomLeftButtons:@[self.backButton]];
  94. }
  95. }
  96. - (void)backAction {
  97. if (self.webView.canGoBack) {
  98. [self.webView goBack];
  99. }else{
  100. [self.navigationController popViewControllerAnimated:YES];
  101. }
  102. }
  103. - (void)closeAction {
  104. [self.navigationController popViewControllerAnimated:YES];
  105. }
  106. - (WKWebView *)webView{
  107. if (!_webView) {
  108. _webView = [[WKWebView alloc]initWithFrame:CGRectMake(0, NavBarHeight, SCREEN_WIDTH, SCREEN_HEIGHT-NavBarHeight)];
  109. _webView.backgroundColor = [UIColor colorWithRed:240.0/255 green:240.0/255 blue:240.0/255 alpha:1.0];
  110. _webView.navigationDelegate = self;
  111. _webView.UIDelegate = self;
  112. //使用kvo监听进度
  113. [_webView addObserver:self forKeyPath:NSStringFromSelector(@selector(estimatedProgress)) options:0 context:nil];
  114. //手势触摸滑动
  115. _webView.allowsBackForwardNavigationGestures = YES;
  116. //自适应
  117. [_webView sizeToFit];
  118. //自适应
  119. [_webView sizeToFit];
  120. }
  121. return _webView;
  122. }
  123. #pragma mark-
  124. #pragma mark- SetupConstraints
  125. /** 加载配置以及视图添加*/
  126. - (void)loadSubViewsConfiguration{
  127. [self.view addSubview:self.webView];
  128. }
  129. - (UIButton *)backButton {
  130. if (!_backButton) {
  131. _backButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
  132. [_backButton setImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];
  133. [_backButton addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside];
  134. }
  135. return _backButton;
  136. }
  137. - (UIButton *)closeButton {
  138. if (!_closeButton) {
  139. _closeButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
  140. [_closeButton setImage:[UIImage imageNamed:@"close_web"] forState:UIControlStateNormal];
  141. [_closeButton addTarget:self action:@selector(closeAction) forControlEvents:UIControlEventTouchUpInside];
  142. }
  143. return _closeButton;
  144. }
  145. - (UIProgressView *)progressView{
  146. if (!_progressView) {
  147. _progressView = [[UIProgressView alloc]initWithProgressViewStyle:UIProgressViewStyleDefault];
  148. _progressView.frame = CGRectMake(0, NavBarHeight, self.view.frame.size.width, 2);
  149. //设置进度条的色彩
  150. [_progressView setTrackTintColor:[UIColor colorWithRed:240.0/255 green:240.0/255 blue:240.0/255 alpha:1.0]];
  151. _progressView.progressTintColor = [UIColor homeRedColor];
  152. }
  153. return _progressView;
  154. }
  155. //观察的移除
  156. - (void)dealloc{
  157. [self.webView removeObserver:self forKeyPath:NSStringFromSelector(@selector(estimatedProgress))];
  158. }
  159. - (void)didReceiveMemoryWarning {
  160. [super didReceiveMemoryWarning];
  161. // Dispose of any resources that can be recreated.
  162. }
  163. /*
  164. #pragma mark - Navigation
  165. // In a storyboard-based application, you will often want to do a little preparation before navigation
  166. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  167. // Get the new view controller using [segue destinationViewController].
  168. // Pass the selected object to the new view controller.
  169. }
  170. */
  171. @end