我有一个 UIWebView
我通过它自动滚动
// First the timer is called.
[NSTimer scheduledTimerWithTimeInterval:scrollerTimerInterval
target:self
selector:@selector(_autoScrollTimerMethod)
userInfo:nil
repeats:YES];
- (void) _autoScrollTimerMethod{
scrollerTimerCount++;
CGFloat offset = scrollerTimerCount * 1;
uiWebView.scrollView.contentOffset = CGPointMake(0, offset);
}
uiWebView 的类型为
UIWebView
这段代码会自动滚动 uiWebView,但它太慢了。程序的另一部分有闪烁,注释掉这段代码时不会出现闪烁。
欢迎任何建议。
干杯,
最佳答案
所以我想我想出了一个可以满足你需要的解决方案。基本上,我在 UIScrollView 中创建了一个 UIWebView。我必须这样做,因为否则 UIWebView 本身不会完全呈现动画的原因,并且你会得到大片白色(webview 的未渲染部分)。因此,在 Interface Builder 中,添加一个带有 webview 的 scrollview,然后将它们都连接到 IBOutlets。
你还需要你的 ViewController 来实现 UIWebViewDelegate
协议(protocol)。
@interface ViewController : UIViewController <UIWebViewDelegate>
@end
在您的 viewDidLoad 中,(或界面构建器)连接 webview 的委托(delegate):
self.webView.delegate = self;
然后在 webview 中加载一个 URL:
[self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://stackoverflow.com/questions/"]]];
一旦 webview 完成加载,我们将把它的 frame 展开到 contentSize 的全高,使它被完全渲染。不,如果您对非常大的网页执行此操作,这将是一个内存滚刀,因此请谨慎使用。然后我们将设置 ScrollView 的 contentSize 以匹配,然后简单地将内容偏移设置到末尾,无论您想要什么持续时间。
- (void)webViewDidFinishLoad:(UIWebView *)webView {
webView.frame = CGRectMake(webView.frame.origin.x, webView.frame.origin.y, webView.scrollView.contentSize.width, webView.scrollView.contentSize.height);
self.scrollView.contentSize = webView.scrollView.contentSize;
[UIView animateWithDuration:10 delay:0 options:UIViewAnimationOptionBeginFromCurrentState animations:^{
[self.scrollView setContentOffset:CGPointMake(0, self.scrollView.contentSize.height - self.scrollView.frame.size.height)];
} completion:^(BOOL finished) {
}];
}
关于ios - 使用 UIWebView 快速滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25319664/