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

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

问题描述

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

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 来在页面加载时为您提供一些可以查看的内容.Mail 还使用虚假的进度视图.自己去试试吧.这就是 Apple 的虚假进度视图的工作原理:

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天全站免登陆