Detect UITextView scroll location

2019-02-16 15:12发布

I am trying to implement a form of a Terms & Conditions page where the "Proceed" button is only enabled once the user has scrolled to the bottom of a UITextView. So far I have set my class as a UIScrollView delegate & have implemented the method below:

- (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])
    {
    }    
}

I have commented the if statement because I am not sure how to check if the UITextView is at the bottom.

3条回答
你好瞎i
2楼-- · 2019-02-16 15:32

A Swift version for this question:

func scrollViewDidScroll(_ scrollView: UIScrollView) {

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

        print( "View scrolled to the bottom" )

    }
}
查看更多
再贱就再见
3楼-- · 2019-02-16 15:32

This should solve it. It works. I am using it.

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView 
{
    float bottomEdge = scrollView.contentOffset.y + scrollView.frame.size.height;
    if (bottomEdge >= scrollView.contentSize.height) 
    {
        // we are at the end
    }
}
查看更多
迷人小祖宗
4楼-- · 2019-02-16 15:49

UITextView is a UIScrollView subclass. Therefore the UIScrollView delegate method you are using is also available when using UITextView.

Instead of using scrollViewDidEndDecelerating, you should use scrollViewDidScroll, as the scrollview may stop scrolling without deceleration.

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (scrollView.contentOffset.y >= scrollView.contentSize.height - scrollView.frame.size.height)
    {
        NSLog(@"at bottom");
    }
}
查看更多
登录 后发表回答