Created
June 10, 2014 08:03
-
-
Save cobysy/63e807a9ee14b11ee50b to your computer and use it in GitHub Desktop.
Snap table to top most cell when dragging
This file contains 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 decelerating, let scrollViewDidEndDecelerating: handle it | |
if (decelerate == NO) | |
[self snapTable]; | |
} | |
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView { | |
[self snapTable]; | |
} | |
- (void)snapTable { | |
NSIndexPath *pathForTopCell = [self.tableView indexPathForRowAtPoint:self.tableView.bounds.origin]; | |
CGRect topCellRect = [self.tableView rectForRowAtIndexPath:pathForTopCell]; | |
float yOffset = self.tableView.contentOffset.y; | |
float positionInCell = fmodf(yOffset / topCellRect.size.height, 1); | |
if (positionInCell > 0.5f) | |
pathForTopCell = [NSIndexPath indexPathForRow:pathForTopCell.row + 1 inSection:pathForTopCell.section]; | |
[self.tableView scrollToRowAtIndexPath:pathForTopCell atScrollPosition:UITableViewScrollPositionTop animated:YES]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment