iOS:检测到达顶部或底部的UIWebView [英] iOS : detect UIWebView reaching the top or bottom
问题描述
如何检测到达顶部或底部的UIWebView?
因为当我到达底部时我需要触发一个动作。
How can I detect UIWebView reaching the top or bottom? Because I need to trigger an action when I reach the bottom.
那可行吗?
推荐答案
首先,你不应该把 UIWebView
放在 UIScrollView
如 UIWebView
文档中所述。
First of all, you should not put your UIWebView
inside a UIScrollView
as mentionned in the UIWebView
documentation.
重要提示:不应在UIScrollView对象中嵌入UIWebView或UITableView对象。如果这样做,可能会导致意外行为,因为两个对象的触摸事件可能混淆和错误处理。
Important: You should not embed UIWebView or UITableView objects in UIScrollView objects. If you do so, unexpected behavior can result because touch events for the two objects can be mixed up and wrongly handled.
相反,你可以通过 UIWebView
属性访问 UIScrollView
。您的视图控制器可以实现 UIScrollViewDelegate
。
Instead, you can access the UIScrollView
through the UIWebView
properties. Your view controller can implement the UIScrollViewDelegate
.
@interface MyViewController : UIViewController<UIScrollViewDelegate>
@end
然后你可以挂钩到UIScrollView:
Then you can hook to the UIScrollView:
- (void)viewDidLoad
{
[super viewDidLoad];
// Set self as scroll view protocol
webView.scrollView.delegate = self;
}
然后最终检测到达到的顶部和底部:
Then finally detect the top and bottom being reached:
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
if(scrollView.contentOffset.y
>=
(scrollView.contentSize.height - scrollView.frame.size.height)){
NSLog(@"BOTTOM REACHED");
}
if(scrollView.contentOffset.y <= 0.0){
NSLog(@"TOP REACHED");
}
}
Voila,您可以检测到何时达到顶部和底部。
Voila, you can detect when top and bottom are reached.
这篇关于iOS:检测到达顶部或底部的UIWebView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!