3

在我的webViewDidFinishLoad方法中,我有以下代码:

- (void)webViewDidFinishLoad:(UIWebView *)webView {   
    if (hasPressed == 1) {
        hasPressed = 0;
        isBlocked = 0;
    }
    else if (hasPressed == 0 && hasDroppedDown == 0) {
        if (viewState != kStateWeather || isBlocked == 0) {
        [UIView animateWithDuration:0.5 
                              delay:0.0 options:UIViewAnimationCurveLinear animations:^{
                                  //Animations
                              }
                         completion:^(BOOL finished){
                             hasDroppedDown = 1;
                         }];
        }
    }
}

正如你所看到的,如果这个方法被快速调用,例如一个有重定向的网站,我的elsein myelse if变得毫无用处。我如何阻止它被如此迅速地调用?

4

2 回答 2

1

不幸的是,你不能这样做。当新的 URL 开始加载到UIWebView. 相反,你可以做一件事。实现委托功能

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType

当新框架即将开始加载到UIWebView. navigationType您可以通过检查哪些可以获得值来确定要加载哪种类型的请求

UIWebViewNavigationTypeLinkClicked
UIWebViewNavigationTypeFormSubmitted
UIWebViewNavigationTypeBackForward
UIWebViewNavigationTypeReload
UIWebViewNavigationTypeFormResubmitted
UIWebViewNavigationTypeOther

注意YES如果您允许 webview 加载页面,则返回NO

希望这对您有所帮助。

于 2012-07-12T11:48:02.537 回答
1
- (void)main { //being called every 0.2
    t+=0.2;
}
- (void)webViewDidFinishLoad:(UIWebView *)webView {   
    loadImg.hidden = 1;
    m_activity.hidden= TRUE;     
    [m_activity stopAnimating];
    if (t>0.5) {
        [self finishedLoading];
        t=0;
    }
}
- (void)finishedLoading {
    if (hasPressed == 1) {
        hasPressed = 0;
        isBlocked = 0;
    }
    else if (hasPressed == 0 && hasDroppedDown == 0) {
        if (viewState != kStateWeather || isBlocked == 0) {
            [UIView animateWithDuration:0.5 
                                  delay:0.0 options:UIViewAnimationCurveLinear animations:^{
                                      //Animations
                                  }
                             completion:^(BOOL finished){
                                 hasDroppedDown = 1;
                             }];
        }
    }
}
于 2012-07-12T11:58:57.510 回答