检测UITextView滚动位置

时间:2012-12-20 10:23:03

标签: ios cocoa uitextview uitextviewdelegate

我正在努力实施条款和条款的形式只有在用户滚动到UITextView底部后才会启用“继续”按钮的条件页面。到目前为止,我已将我的课程设置为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是否在底部。

3 个答案:

答案 0 :(得分:15)

UITextView是一个UIScrollView子类。因此,在使用UITextView时,您使用的UIScrollView委托方法也可用。

不应使用scrollViewDidEndDecelerating,而应使用scrollViewDidScroll,因为滚动视图可能会停止滚动而不会减速。

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (scrollView.contentOffset.y >= scrollView.contentSize.height - scrollView.frame.size.height)
    {
        NSLog(@"at bottom");
    }
}

答案 1 :(得分:2)

这个问题的Swift版本:

func scrollViewDidScroll(_ scrollView: UIScrollView) {

    if scrollView.contentOffset.y >= scrollView.contentSize.height - scrollView.frame.size.height {

        print( "View scrolled to the bottom" )

    }
}

答案 2 :(得分:0)

这应该解决它。有用。我正在使用它。

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView 
{
    float bottomEdge = scrollView.contentOffset.y + scrollView.frame.size.height;
    if (bottomEdge >= scrollView.contentSize.height) 
    {
        // we are at the end
    }
}