检测UITextView滚动位置
我试图实现一个条款和条件页面的forms,只有当用户滚动到UITextView的底部时,才能启用“继续”button。 到目前为止,我已经把我的类设置为一个UIScrollView委托,并实现了下面的方法:
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView { NSLog(@"Checking if at bottom of UITextView"); CGPoint bottomOffset = CGPointMake(0,self.warningTextView.frame.size.height); //if ([[self.warningTextView contentOffset] isEqualTO:bottomOffset]) { } }
我已经评论了if语句,因为我不确定如何检查UITextView是否在底部。 有没有人有什么build议?
非常感谢,詹姆斯
UITextView是一个UIScrollView的子类。 因此,您使用的UIScrollView委托方法在使用UITextView时也是可用的。
而不是使用scrollViewDidEndDecelerating
,你应该使用scrollViewDidScroll
,因为滚动视图可能停止滚动而不减速。
- (void)scrollViewDidScroll:(UIScrollView *)scrollView { if (scrollView.contentOffset.y >= scrollView.contentSize.height - scrollView.frame.size.height) { NSLog(@"at bottom"); } }
这个问题的Swift版本:
func scrollViewDidScroll(_ scrollView: UIScrollView) { if (scrollView.contentOffset.y >= scrollView.contentSize.height - scrollView.frame.size.height) { print( "View scrolled to the bottom" ) } }
这应该解决它。 有用。 我正在使用它。
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView { float bottomEdge = scrollView.contentOffset.y + scrollView.frame.size.height; if (bottomEdge >= scrollView.contentSize.height) { // we are at the end } }