Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use WKWebView to imitate Wechat to load progress bar in iOS

2025-03-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces how to use WKWebView to imitate Wechat in iOS to load the progress bar, which has a certain reference value, and interested friends can refer to it. I hope you can learn a lot after reading this article, and let the editor take you to know it.

The details are as follows

WKWebView adds the estimatedProgress attribute (double type), which we can use to set the UIProgressView

Demo stored on the github code repository

Add a UIProgressView attribute to the page

@ property (nonatomic, strong) WKWebView * mywebView;@property (nonatomic, strong) UIProgressView * progressView;// sets the loading progress bar

Lazy load UIProgressView

-(UIProgressView *) progressView {if (! _ progressView) {_ progressView = [[UIProgressView alloc] initWithProgressViewStyle:UIProgressViewStyleDefault]; _ progressView.frame = CGRectMake (0,64, screen_width, 5); [_ progressView setTrackTintColor: [UIColor colorWithRed:240.0/255 green:240.0/255 blue:240.0/255 alpha:1.0]]; _ progressView.progressTintColor = [UIColor greenColor];} return _ progressView;}

Kvo adds monitoring when initializing WKWebView (when I am loading lazily)

[_ mywebView addObserver:self forKeyPath:NSStringFromSelector (@ selector (estimatedProgress)) options:0 context:nil]

Hide the progress bar when the page starts to load

/ / start loading-(void) webView: (WKWebView *) webView didStartProvisionalNavigation: (WKNavigation *) navigation {/ / when you start loading, let the progress bar display self.progressView.hidden = NO;}

Kvo listens for progress

/ / kvo snooping progress-(void) observeValueForKeyPath: (NSString *) keyPath ofObject: (id) object change: (NSDictionary *) change context: (void *) context {if ([keyPath isEqualToString:NSStringFromSelector (@ selector (estimatedProgress))] & & object = = self.mywebView) {[self.progressView setAlpha:1.0f]; BOOL animated = self.mywebView.estimatedProgress > self.progressView.progress; [self.progressView setProgress:self.mywebView.estimatedProgress animated:animated] If (self.mywebView.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];}}

Remove listeners in the dealloc method

-(void) dealloc {[self.mywebView removeObserver:self forKeyPath:NSStringFromSelector (@ selector (estimatedProgress))];}

Thank you for reading this article carefully. I hope the article "how to use WKWebView to imitate Wechat to load progress bar in iOS" shared by the editor will be helpful to everyone. At the same time, I also hope that you will support and follow the industry information channel. More related knowledge is waiting for you to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report