您好,我是编程新手,我正在尝试在 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 UIProgressView
s 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:
要实现这一点,您必须手动为 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!