这是我的代码:
@IBOutlet weak var webView: WKWebView!
@IBOutlet weak var progressView: UIProgressView!
override func viewDidLoad() {
super.viewDidLoad()
webView.uiDelegate = self
webView.navigationDelegate = self
webView.addObserver(self, forKeyPath: "estimatedProgress", options: .new, context: nil)
}
override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
if (keyPath == "estimatedProgress") {
progressView.setProgress(Float(webView.estimatedProgress), animated: true)
print(webView.estimatedProgress)
}
}
Run Code Online (Sandbox Code Playgroud)
但是,估计进度只显示两个浮点数(0.1和1.0)而且我觉得它不起作用.我使用了Alamofire的进度,它改变了几毫秒,使我的UI更好,但这段代码不能正常工作......
有谁可以帮助我进行webView的进展?
Vla*_*lav 11
新的 Swift KVO 方法
class ViewController: UIViewController {
private lazy var webView = WKWebView()
private var observation: NSKeyValueObservation?
override func viewDidLoad() {
super.viewDidLoad()
observation = webView.observe(\WKWebView.estimatedProgress, options: .new) { _, change in
print("Loaded: \(change)")
}
}
deinit {
self.observation = nil
}
}
Run Code Online (Sandbox Code Playgroud)
这应该在Swift 5上正常工作:
@IBOutlet weak var webView: WKWebView!
@IBOutlet weak var progressView: UIProgressView!
private var observation: NSKeyValueObservation? = nil
override func viewDidLoad() {
super.viewDidLoad()
// load website or HTML page
webView.load(NSURLRequest(url: URL(string: "https://www.apple.com")!) as URLRequest)
// add observer to update estimated progress value
observation = webView.observe(\.estimatedProgress, options: [.new]) { _, _ in
self.progressView.progress = Float(self.webView.estimatedProgress)
}
}
deinit {
observation = nil
}
Run Code Online (Sandbox Code Playgroud)
小智 7
Swift 4.0
@IBOutlet weak var webView: WKWebView!
@IBOutlet weak var progressView: UIProgressView!
override func viewDidLoad() {
super.viewDidLoad()
// load website or HTML page
self.webView.load(NSURLRequest(url: URL(string: "https://www.apple.com")!) as URLRequest);
//add observer to get estimated progress value
self.webView.addObserver(self, forKeyPath: "estimatedProgress", options: .new, context: nil);
}
// Observe value
override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
if keyPath == "estimatedProgress" {
print(self.webView.estimatedProgress);
self.progressView.progress = Float(self.webView.estimatedProgress);
}
}
Run Code Online (Sandbox Code Playgroud)
小智 5
KVO 的 Objective-C 实现:
-(void)viewDidLoad {
[super viewDidLoad];
[self.webView addObserver:self forKeyPath:NSStringFromSelector(@selector(estimatedProgress)) options:NSKeyValueObservingOptionNew context:NULL];
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
if ([keyPath isEqualToString:NSStringFromSelector(@selector(estimatedProgress))] && object == self.webView) {
self.progressView.alpha = 1.0;
[self.progressView setProgress:self.webView.estimatedProgress animated:YES];
if(self.webView.estimatedProgress >= 1.0f) {
[UIView animateWithDuration:0.3 delay:0.3 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];
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4131 次 |
| 最近记录: |