检测UITextView滚动位置

Jam*_*LCQ 10 cocoa uitextview uitextviewdelegate ios

我正在尝试实现一个条款和条件页面的形式,其中"继续"按钮仅在用户滚动到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])
    {
    }    
}
Run Code Online (Sandbox Code Playgroud)

我已经评论了if语句,因为我不确定如何检查UITextView是否位于底部.

how*_*ghk 17

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

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

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

  • 你所关注的`scrollView`是变量名,只要你改变方法名中的那个,就可以调用它.例如,您可以将其命名为`textView`,并将方法名称更改为` - (void)scrollViewDidScroll:(UIScrollView*)textView`.或者,您可以直接引用您的UITextView,并将if行更改为`if(self.warningTextView.contentOffset.y> = self.warningTextView.contentSize.height - self.warningTextView.frame.size.height)`. (2认同)

Bri*_*ure 7

这个问题的 Swift 版本:

func scrollViewDidScroll(_ scrollView: UIScrollView) {

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

        print( "View scrolled to the bottom" )

    }
}
Run Code Online (Sandbox Code Playgroud)