UIWebView滚动时调用方法
我有一个包含大量文本内容的 UIWebView。我需要能够在每次移动时获取 UIWebView 的位置。我使用这段代码来说明这一点:
pageYOffset = [[webView stringByEvaluatingJavaScriptFromString:@"window.pageYOffset"] intValue];
现在我只需要做到这一点,以便每次 UIWebView 位置移动或有任何滚动时都会更新此变量值。是否可以在 UIWebView 滚动时调用方法?
I have a UIWebView that contains a lot of text content. I need to be able to get the location of the UIWebView every time it moves. I am using this code to get the point:
pageYOffset = [[webView stringByEvaluatingJavaScriptFromString:@"window.pageYOffset"] intValue];
now I just need to make it so that this variables value is updated everytime the UIWebView position moves, or there is any scrolling. Is it possible to call a method whenever the UIWebView scrolls?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您应该扩展UIWebView,如下所示
,并在控制器上实现scrollViewDidScroll,如下所示:
并且不要忘记将控制器设置为UIWebView的委托属性。
You should extend UIWebView, like below
and implement scrollViewDidScroll on your controller like:
and don't forget to set your controller to the delegate property of UIWebView.
我相信,如果您的标头将您的 VC 列为 UIScrollViewDelegate,您可以使用
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
方法来查找何时滚动。I believe that if your header lists your VC as a UIScrollViewDelegate, you can use the
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
method to find out when it scrolls.这是不正确的。如果您将使用:
当尝试在 iOS7 及更多版本上的页面上输入数据时,您将失去焦点
您需要为 UIWevView 实现自定义类并覆盖scrollViewDidScroll:
This is not correct. If you will use:
You will lose focus when try to enter data on page on iOS7 and more
You need to implement custom class for UIWevView and overwrite scrollViewDidScroll:
如果您的要求是在用户滚动时继续加载 webview 那么您应该将代码粘贴到
if your requirement is to keep loading webview when user scrolls then you should paste your code into