酷店

KDPWebVC.m 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. //
  2. // KDPWebVC.m
  3. // KuDianProject
  4. //
  5. // Created by 学丽 on 2019/7/9.
  6. // Copyright © 2019 KDP. All rights reserved.
  7. //
  8. #import "KDPWebVC.h"
  9. @interface KDPWebVC ()<WKUIDelegate,WKNavigationDelegate,WKScriptMessageHandler>
  10. @property(nonatomic,strong)WKWebView *webViews;
  11. @property(nonatomic,strong)UIButton *returnBtn;
  12. @property(nonatomic,strong)UIButton *closeBtn;
  13. @property(nonatomic,strong)UIButton *refreshBtn;
  14. @property(nonatomic,strong)UIProgressView *progressBarView;
  15. @end
  16. @implementation KDPWebVC
  17. - (void)viewDidLoad {
  18. [super viewDidLoad];
  19. self.view.backgroundColor=[UIColor colorWithHexString:@"#F4F4F4"];
  20. [self.navBar addleftReturnButton:self selector:@selector(returnClickBtn)];
  21. self.webViews =[[WKWebView alloc]initWithFrame:CGRectMake(0, KDNavBarHeight, SCREEN_WIDTH, SCREEN_HEIGHT-KDNavBarHeight)];
  22. self.webViews.backgroundColor=[UIColor colorWithHexString:@"F4F4F4"];
  23. self.webViews.navigationDelegate=self;
  24. self.webViews.UIDelegate=self;
  25. [ self.webViews addObserver:self forKeyPath:NSStringFromSelector(@selector(estimatedProgress)) options:0 context:nil];
  26. //手势触摸滑动
  27. self.webViews.allowsBackForwardNavigationGestures = YES;
  28. //自适应
  29. [ self.webViews sizeToFit];
  30. //自适应
  31. [ self.webViews sizeToFit];
  32. [self.view addSubview: self.webViews];
  33. self.progressBarView = [[UIProgressView alloc]initWithProgressViewStyle:UIProgressViewStyleDefault];
  34. self.progressBarView.frame = CGRectMake(0, KDNavBarHeight, self.view.frame.size.width, 2);
  35. //设置进度条的色彩
  36. [self.progressBarView setTrackTintColor:[UIColor colorWithRed:240.0/255 green:240.0/255 blue:240.0/255 alpha:1.0]];
  37. self.progressBarView.progressTintColor = [UIColor colorWithHexString:ThemeColor];
  38. [self.view addSubview:self.progressBarView];
  39. if (@available(iOS 11.0,*)) {
  40. self.webViews.scrollView.contentInsetAdjustmentBehavior=UIScrollViewContentInsetAdjustmentNever;
  41. }else{
  42. self.automaticallyAdjustsScrollViewInsets = NO;
  43. }
  44. [self loadUrl];
  45. }
  46. -(void)loadUrl
  47. {
  48. if (self.webUrl.length>0) {
  49. NSURLRequest *request=[NSURLRequest requestWithURL:[NSURL URLWithString:self.webUrl]];
  50. [self.webViews loadRequest:request];
  51. }
  52. }
  53. - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context{
  54. if ([keyPath isEqualToString:NSStringFromSelector(@selector(estimatedProgress))] && object == self.webViews ) {
  55. [self.progressBarView setAlpha:1.0];
  56. [self.progressBarView setProgress:self.webViews.estimatedProgress ];
  57. if (self.webViews.estimatedProgress >= 1.0) {
  58. [UIView animateWithDuration:0.7 animations:^{
  59. [self.progressBarView setProgress:1.0 animated:YES];
  60. [self.progressBarView setAlpha:0.0];
  61. }];
  62. }
  63. }else{
  64. [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
  65. }
  66. }
  67. /**
  68. 当网页加载完成的时候调用
  69. @param webView web描述
  70. @param navigation 导航的描述
  71. */
  72. - (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation{
  73. self.navigationItem.title=webView.title;
  74. self.navBar.navTitleLabel.text=webView.title;
  75. [[UIApplication sharedApplication]setNetworkActivityIndicatorVisible:NO];
  76. }
  77. - (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(WKNavigation *)navigation{
  78. //开始加载的时候,让进度条显示
  79. self.progressBarView.hidden = NO;
  80. self.progressBarView.progress = 0;
  81. self.progressBarView.alpha = 1.0;
  82. [UIView animateWithDuration:0.8 animations:^{
  83. self.progressBarView.progress = 0.6;
  84. }];
  85. }
  86. //观察的移除
  87. - (void)dealloc{
  88. [self.webViews removeObserver:self forKeyPath:NSStringFromSelector(@selector(estimatedProgress))];
  89. }
  90. -(void)returnClickBtn
  91. {
  92. [self.navigationController popViewControllerAnimated:YES];
  93. }
  94. -(void)viewWillAppear:(BOOL)animated
  95. {
  96. [super viewWillAppear:animated];
  97. self.navigationController.navigationBar.hidden=YES;
  98. self.tabBarController.tabBar.hidden=YES;
  99. }
  100. -(void)viewWillDisappear:(BOOL)animated
  101. {
  102. [super viewWillDisappear:animated];
  103. self.tabBarController.tabBar.hidden=NO;
  104. }
  105. @end