Skip to content

Instantly share code, notes, and snippets.

@psobko
Created October 29, 2013 20:45
Show Gist options
  • Save psobko/7222303 to your computer and use it in GitHub Desktop.
Save psobko/7222303 to your computer and use it in GitHub Desktop.
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate(BOOL)decelerate {
if (decelerate == NO) {
[self autoAdjustScrollToTop];
}
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
[self autoAdjustScrollToTop];
}
- (void)autoAdjustScrollToTop {
// compare the top two visible rows to the current content offset
// and auto scroll so that the best row is snapped to top
NSArray *visibleRows = [self.tableView indexPathsForVisibleRows];
NSIndexPath *firstPath = visibleRows[0];
NSIndexPath *secondPath = visibleRows[1];
CGRect firstRowRect = [self.tableView rectForRowAtIndexPath:firstPath];
[self.tableView scrollToRowAtIndexPath:(firstRowRect.origin.y > self.tableView.contentOffset.y ? firstPath : secondPath) atScrollPosition:UITableViewScrollPositionTop animated:YES];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment