Skip to content

Instantly share code, notes, and snippets.

@cobysy
Created June 10, 2014 08:03
Show Gist options
  • Save cobysy/63e807a9ee14b11ee50b to your computer and use it in GitHub Desktop.
Save cobysy/63e807a9ee14b11ee50b to your computer and use it in GitHub Desktop.
Snap table to top most cell when dragging
- (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