简体   繁体   中英

How can I detect when a UITableView has been scroll past the very bottom/top?

I've got a view that has 3 tableView's. One is the "Main Table View", and then I have an 'Answers Table View' and 'Percentage Table View'.

When the screens loads, the Main Table View occupies the top 95% of the screen. The bottom of the screen is a UIView containing 2 buttons. "Answers" and "Percentage".

The way it works, is if I click "Percentage" it changes the height of the Main Table View to 0, and gives that height to the Answers Table View. This animates the "Answers/Percentage" View to the top, and reveals either the Answers or Percentage TableView below it.

Here's an example:

在此处输入图片说明

As you can see, I click on "Percentage" which animates it up. If you click on "Percentage" again it animates it back down.

However, what I want to do is if the "Answers/Percentage" view is at the bottom of the View, and the user scrolls the Main Feed UP reaches the very end of the tableView's contents (not just the end, but the end and a little bit more), I want to animate it up like in the .gif.

Similarly, if the "Answers/Percentage" is at the top, and the user scrolls the lower "Answers Table View" down past a certain point where there is no more data above, it will completely animate.

Also, I do not ever want the "Answers/Percentage" view to be in the middle, and showing a tableview both top and bottom. All one, or the other, but not a bit of both. Which I have right now.

What I need to know is... how can I detect if the user has scrolled past the very top or very bottom of the table view +30 pixels for example, to initiate my animation?

You can use the contentOffset property of the table view.

if(tableView.contentOffset.y >= (tableView.contentSize.height - tableView.frame.size.height)) {
    // Start the animation
}

I haven't tested this, but let me know if it works.

Making another answer based on sublimepremise 's approach because it's easier to format and post code that way. The base idea is to check the table view's .contentOffset.y , eg by implementing scrollViewDidScroll in its delegate, and triggering your animations accordingly.

It may have a bit of a QnD feel to it, but if you need to also abort the user's dragging action when triggering the animation, an easy way to do so would be by "resetting" the table view's gesture recognizer. In code that could look something like this:

static const CGFloat kChrisTableViewAnimationThreshold = 30.0f;

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    if (scrollView == self.topTableView) {
        if([self scrolledPastBottomThresholdInTableView:self.topTableView]) {
            // Start the animation
            // ...

            // Toggling the table view's pangGestureRecognizer off and on cancels the gesture
            self.topTableView.panGestureRecognizer.enabled = NO;
            self.topTableView.panGestureRecognizer.enabled = YES;
        }
    }
}

- (BOOL)scrolledPastBottomThresholdInTableView:(UITableView *)tableView {
    return (tableView.contentOffset.y - kChrisTableViewAnimationThreshold >= (tableView.contentSize.height - tableView.frame.size.height));
}

Note that this illustration obviously only covers recognizing when the top table view is scrolled past its bottom end, but adapting it for handling other scenarios should be pretty straightforward.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM