带有进度栏的UIWebView [英] UIWebView with Progress Bar

查看:83
本文介绍了带有进度栏的UIWebView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是编程新手,我正在尝试在Xcode上制作我的第一个iPhone应用程序. 我的应用程序包含一个按钮,按下该按钮可打开UIWebView并加载主页. 现在,我也想像Safari也使用的那样向WebView添加一个Progress View,它指示了加载页面的进度.我该怎么办?
到目前为止,我的代码用于将URL加载到UIWebView中:

Hi I'm new to programming and I'm trying to make my first app for iPhones on Xcode. My app contains of a button which opens a UIWebView when pressed and loads up a homepage. Now I also want to add a Progress View to the WebView like Safari also uses, which indicates the progress of loading the page. How can I do that?
My code so far for loading the URL in the UIWebView:

.h

IBOutlet UIWebView *webView;

.m

- (void)viewDidLoad
{
[webView loadRequest:[NSURLRequest requestWithURL: [NSURL URLWithString:@"http:/www.google.com/"]]];

感谢您的帮助!

推荐答案

要获得准确的UIProgressView,您需要执行以下任务:

To have an accurate UIProgressView, you need to have some task that:

  • 您可以从尚不完善的信息中获取信息
  • 根据该信息将其完整性"量化为百分比.

现在,当您加载UIWebView时,那是不可能的.苹果也没有这样做. Apple经常在页面加载时使用伪造的UIProgressView给您一些东西.邮件还使用伪造的进度视图.亲自去尝试一下.这是苹果公司虚假进度视图的工作方式:

Now when you are loading your UIWebView, thats not possible. And Apple doesn't do it either. Apple often uses fake UIProgressViews to give you something to look at while the page is loading. Mail also uses fake progress views. Go try it out for yourself. This is how Apple's fake progress views work:

  • 进度视图开始以缓慢,恒定的速度移动
  • 如果任务在栏完成之前完成,它会突然消失到其余部分,直到消失为止100%
  • 如果任务花费很长时间,则进度视图将以95%的比例停止,并将一直停留在该位置,直到任务完成为止.

要实现此目的,您将必须手动为progressView设置动画.您可以将其子类化,但是对您来说可能会有点高级.最简单的方法是这样:

To achieve this, you will have to animate the progressView manually. You could subclass it but that would probably be a bit advanced for you. The simplest way would be this:

在myViewController.h

In myViewController.h

@interface myViewController : UIViewController {
     BOOL theBool;
     //IBOutlet means you can place the progressView in Interface Builder and connect it to your code
     IBOutlet UIProgressView* myProgressView;
     NSTimer *myTimer;
}
@end

在myViewController.m

In myViewController.m

#import "myViewController.h"
@implementation myViewController
- (void)webViewDidStartLoad:(UIWebView *)webView{
     myProgressView.progress = 0;
     theBool = false;
     //0.01667 is roughly 1/60, so it will update at 60 FPS
     myTimer = [NSTimer scheduledTimerWithTimeInterval:0.01667 target:self selector:@selector(timerCallback) userInfo:nil repeats:YES];
}
- (void)webViewDidFinishLoad:(UIWebView *)webView{
     theBool = true;
}
-(void)timerCallback {
    if (theBool) {
         if (myProgressView.progress >= 1) {
              myProgressView.hidden = true;
              [myTimer invalidate];
         }
         else {
              myProgressView.progress += 0.1;
         }
    }
    else {
         myProgressView.progress += 0.05;
         if (myProgressView.progress >= 0.95) {
              myProgressView.progress = 0.95;
         }
    }
}
@end

然后,在完成任务的地方,设置theBool = true;,进度视图将自行处理.更改if语句中的值以控制动画的速度.

Then, where your task gets completed, set theBool = true; and the progress view will take care of itself. Change the values in the if statement thing to control the speed of the animation.

这篇关于带有进度栏的UIWebView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆