DYFProgressView 1.2.1

DYFProgressView 1.2.1

Tenfay 维护。



  • Tenfay

如果此项目能帮助到你,就请你给一颗星。谢谢!(If this project can help you, please give it a star. Thanks!)

License MIT  CocoaPods  CocoaPods 

DYFProgressView

超好用的进度条和网页进度条,操作简单好使用。(Super useful progress bar and web page progress bar, the operation is simple and easy to use.)

组 (ID:614799921)

安装

使用 CocoaPods

pod 'DYFProgressView', '~> 1.1.0'

# Installs lastest version.
pod 'DYFProgressView'

预览

用法

  1. 实例化
// Lazy load
- (DYFWebProgressView *)progressView {
    
    if (!_progressView) {
        CGFloat w = self.navigationBar.bounds.size.width;
        CGFloat h = 3.f;
        CGFloat x = 0.f;
        CGFloat y = self.navigationBar.bounds.size.height - h;

        _progressView = [[DYFWebProgressView alloc] initWithFrame:CGRectMake(x, y, w, h)];
        _progressView.lineWidth = 3.f;
        _progressView.lineColor = [UIColor colorWithRed:RGB_V(10) 
                                                  green:RGB_V(115) 
                                                   blue:RGB_V(255) 
                                                  alpha:1];
    }
    
    return _progressView;
}

- (UINavigationBar *)navigationBar {
    return self.navigationController.navigationBar;
}
  1. 添加到父视图
// 在开始加载进度前,调用它
- (void)loadView {
    [super loadView];
    [self addProgressView];
}

// 添加到父视图。
- (void)addProgressView {
    if (!_progressView) {
        [self.navigationBar addSubview:self.progressView];
    }
}
  1. 开始加载进度
[self.progressView startLoading];
  1. 结束加载进度
[self.progressView endLoading];

如何嵌入 WKWebView ?

  1. 当主框架导航开始时被调用。
- (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(WKNavigation *)navigation {
    // didStartProvisionalNavigation.

    NSURL *aURL = [webView.URL copy];
    NSLog(@"%s url: %@", __FUNCTION__, aURL);

    [self.progressView startLoading];
}
  1. 接收到主框架的地址重定向时被调用。
- (void)webView:(WKWebView *)webView didReceiveServerRedirectForProvisionalNavigation:(WKNavigation *)navigation {
    // didReceiveServerRedirectForProvisionalNavigation.
    NSLog(@"%s url: %@", __FUNCTION__, webView.URL);
}
  1. 主框架内容开始到达时被调用。
- (void)webView:(WKWebView *)webView didCommitNavigation:(WKNavigation *)navigation {
    NSLog(@"%s", __FUNCTION__);
}
  1. 主框架导航完成后被调用。
- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation {
    NSLog(@"%s", __FUNCTION__);
    [self.progressView endLoading];
    [self setupNavigationItemTitle];
}
  1. 开始加载主框架数据时发生错误时被调用。
- (void)webView:(WKWebView *)webView didFailProvisionalNavigation:(WKNavigation *)navigation withError:(NSError *)error {
    if (!error) { return; }

    NSString *errMessage = [NSString stringWithFormat:@"%zi, %@", error.code, error.localizedDescription];
    NSLog(@"%s [error]: %@", __FUNCTION__, errMessage);

    [self.progressView endLoading];
}
  1. 在提交主框架导航过程中发生错误时被调用。
- (void)webView:(WKWebView *)webView didFailNavigation:(WKNavigation *)navigation withError:(NSError *)error {
    if (!error) { return; }

    NSString *errMessage = [NSString stringWithFormat:@"%zi, %@", error.code, error.localizedDescription];
    NSLog(@"%s [error]: %@", __FUNCTION__, errMessage);

    [self.progressView endLoading];
}
  1. 决定是否允许或取消导航。
- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler {
    // decidePolicyForNavigationAction.
    [self setupNavigationItemTitle];

    NSURL *aURL = [navigationAction.request.URL copy];
    NSString *aUrl = aURL.absoluteString;
    NSLog(@"%s url: %@", __FUNCTION__, aUrl);

    if (![aUrl isEqualToString:@"about:blank"]) {}

    // Method NO.1: resolve the problem about '_blank'.
    //if (navigationAction.targetFrame == nil) {
        //NSLog(@"- [webView loadRequest:navigationAction.request]");
        //[webView loadRequest:navigationAction.request];
    //}

    decisionHandler(WKNavigationActionPolicyAllow);
}
  1. 创建一个新的WebView。解决 '_blank' 的问题。
// Method NO.2: resolve the problem about '_blank'.
- (WKWebView *)webView:(WKWebView *)webView createWebViewWithConfiguration:(WKWebViewConfiguration *)configuration forNavigationAction:(WKNavigationAction *)navigationAction windowFeatures:(WKWindowFeatures *)windowFeatures {
    // createWebViewWithConfiguration.

    NSURL *aURL = [navigationAction.request.URL copy];
    NSString *aUrl = aURL.absoluteString;
    NSLog(@"%s url: %@", __FUNCTION__, aUrl);

    if (!navigationAction.targetFrame.isMainFrame) {
        NSLog(@"- [webView loadRequest:navigationAction.request]");
        [webView loadRequest:navigationAction.request];
    }

    return nil;
}
  1. 导航项标题显示在导航栏中。
- (void)setupNavigationItemTitle {
    self.navigationItem.title = self.wk_webView.title;
}

代码示例