From f210bfdb840beb1aae243eee0260f8ede2aab795 Mon Sep 17 00:00:00 2001 From: Matt Rubin Date: Thu, 20 Jun 2013 13:35:47 -0700 Subject: [PATCH] Option to snap scrolling to the nearest row boundary --- TimesSquare/TSQCalendarView.h | 7 +++++++ TimesSquare/TSQCalendarView.m | 10 ++++++++++ 2 files changed, 17 insertions(+) diff --git a/TimesSquare/TSQCalendarView.h b/TimesSquare/TSQCalendarView.h index d6f0dab..6b40064 100644 --- a/TimesSquare/TSQCalendarView.h +++ b/TimesSquare/TSQCalendarView.h @@ -71,6 +71,13 @@ */ @property (nonatomic) BOOL pagingEnabled; +/** Whether or not the calendar snaps the nearest edge of a row to the top of its bounds. + + This property is similar to `pagingEnabled`, except the snapping is to rows rather than months. + Paging takes precedence over snapping to row boundaries. + */ +@property (nonatomic) BOOL snapsToRows; + /** The distance from the edges of the view to where the content begins. This property is equivalent to the one defined on `UIScrollView`. diff --git a/TimesSquare/TSQCalendarView.m b/TimesSquare/TSQCalendarView.m index 5678a4e..ce3730b 100644 --- a/TimesSquare/TSQCalendarView.m +++ b/TimesSquare/TSQCalendarView.m @@ -307,6 +307,16 @@ - (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoi } CGRect sectionRect = [self.tableView rectForSection:section]; *targetContentOffset = sectionRect.origin; + } else if (self.snapsToRows) { + // Find the frame of the cell the scroll will end in + NSIndexPath *targetIndexPath = [self.tableView indexPathForRowAtPoint:*targetContentOffset]; + CGRect targetCellRect = [self.tableView rectForRowAtIndexPath:targetIndexPath]; + // Round the scroll finish point to the nearest cell boundary + if (targetContentOffset->y < targetCellRect.origin.y + targetCellRect.size.height/2.) { + targetContentOffset->y = targetCellRect.origin.y; + } else { + targetContentOffset->y = targetCellRect.origin.y + targetCellRect.size.height; + } } }