我的自定义网络浏览器中的加载/刷新按钮
因此,我正在构建一个具有许多自定义功能的自定义网络浏览器,但我遇到了一些非常简单的问题。我有一个 NSTimer:
[NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(checkLoad) userInfo:nil repeats:YES];
这是它调用的方法:
-(void)checkLoad{
if (webView.loading) {
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
//add button to address bar
UIButton *stopButton = [UIButton buttonWithType:UIButtonTypeContactAdd];
stopButton.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 0);
[stopButton addTarget:self action:@selector(stop) forControlEvents:UIControlEventTouchUpInside];
addressBar.rightView = stopButton;
addressBar.rightViewMode = UITextFieldViewModeUnlessEditing;
}
else if (!(webView.loading)) {
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
//add button to address bar
UIButton *refreshButton = [UIButton buttonWithType:UIButtonTypeInfoDark];
refreshButton.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 0);
[refreshButton addTarget:self action:@selector(refresh) forControlEvents:UIControlEventTouchUpInside];
addressBar.rightView = refreshButton;
addressBar.rightViewMode = UITextFieldViewModeUnlessEditing;
}
}
按钮显示得很好,并且在页面加载或未加载时它们会发生变化。但与他们的互动却非常激烈。刷新按钮在大多数情况下都有效,但停止按钮几乎从不工作。我已经在 NSTimer 上调整了时间,让它变得更长或更短,但没有效果。
另外,我知道按钮图像是错误的,但这些只是占位符,直到我可以刷新并停止加载图像(如 safari 中的图像)。
有什么想法吗?我完全愿意做一些完全不同的事情来检测页面何时加载(这实际上是首选,因为如果我可以避免的话,我不希望 NSTimer 每 0.2 秒循环一次,但这是我能找到的唯一方法。
So I'm building a custom web browser with a a lot of custom features, but I'm having a problem with something very simple. I have a NSTimer:
[NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(checkLoad) userInfo:nil repeats:YES];
and here is the method it calls:
-(void)checkLoad{
if (webView.loading) {
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
//add button to address bar
UIButton *stopButton = [UIButton buttonWithType:UIButtonTypeContactAdd];
stopButton.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 0);
[stopButton addTarget:self action:@selector(stop) forControlEvents:UIControlEventTouchUpInside];
addressBar.rightView = stopButton;
addressBar.rightViewMode = UITextFieldViewModeUnlessEditing;
}
else if (!(webView.loading)) {
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
//add button to address bar
UIButton *refreshButton = [UIButton buttonWithType:UIButtonTypeInfoDark];
refreshButton.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 0);
[refreshButton addTarget:self action:@selector(refresh) forControlEvents:UIControlEventTouchUpInside];
addressBar.rightView = refreshButton;
addressBar.rightViewMode = UITextFieldViewModeUnlessEditing;
}
}
the buttons show up just fine, and they change when the page is loading or not loading. but the interaction with them is very spastic. the refresh button works most of the time, but the stop button almost never works. I've played with the timing on the NSTimer making it longer and shorter to no avail.
also, I know the button images are wrong, but those are just placeholder's until I can get the refresh and stop loading images like the ones in safari.
any ideas? I am totally willing to do something completely different to detect when the page is loading (that would actually be preferred as I don't want an NSTimer looping every .2 seconds if I can avoid it but this was the only way I could find.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用
UIWebView
的委托方法有什么问题?请参阅 UIWebViewDelegate。设置每隔一小部分触发一次的计时器,以便您可以检查某些内容是否有“代码异味” '。
What's wrong with using the delegate methods of
UIWebView
? See UIWebViewDelegate.Setting up timers that fire every small fraction of a second so you can check something is a 'code smell'.