在UIWebView滚动时调用方法 [英] Calling a method when UIWebView scrolls
问题描述
我有一个包含大量文本内容的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];
现在我只需要这样做,以便每次UIWebView位置移动时更新此变量值,或者有任何滚动。是否可以在UIWebView滚动时调用方法?
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?
推荐答案
您应该扩展UIWebView,如下所示
You should extend UIWebView, like below
@implementation UIWebView(CustomScroll)
- (void) scrollViewDidScroll:(UIScrollView *)scrollView{
[self.delegate scrollViewDidScroll: scrollView];
}
@end
并在你的控制器上实现scrollViewDidScroll,如:
and implement scrollViewDidScroll on your controller like:
- (void) scrollViewDidScroll:(UIScrollView *)scrollView{
NSLog("webview scrolled");
}
并且不要忘记将控制器设置为UIWebView的委托属性。
and don't forget to set your controller to the delegate property of UIWebView.
这篇关于在UIWebView滚动时调用方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!