Created
October 29, 2013 20:45
-
-
Save psobko/7222303 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (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