From bc3ce07132628a8cc5a04677c1aec06c2534caa1 Mon Sep 17 00:00:00 2001 From: Wenchao Ding Date: Sun, 24 Apr 2016 16:44:11 +0800 Subject: [PATCH] Add -centerForDate: and -frameForDate: Better for a popover --- Example/StoryboardExampleViewController.m | 9 ++------- FSCalendar/FSCalendar.h | 16 +++++++++++++++- FSCalendar/FSCalendar.m | 19 +++++++++++++++++++ 3 files changed, 36 insertions(+), 8 deletions(-) diff --git a/Example/StoryboardExampleViewController.m b/Example/StoryboardExampleViewController.m index f0baf43c..4daae735 100644 --- a/Example/StoryboardExampleViewController.m +++ b/Example/StoryboardExampleViewController.m @@ -92,12 +92,6 @@ - (NSDate *)maximumDateForCalendar:(FSCalendar *)calendar return [calendar dateWithYear:2039 month:5 day:31]; } - -- (void)calendar:(FSCalendar *)calendar didDeselectDate:(NSDate *)date -{ - NSLog(@"Did deselect date %@",[calendar stringFromDate:date]); -} - #pragma mark - FSCalendarDelegate - (BOOL)calendar:(FSCalendar *)calendar shouldSelectDate:(NSDate *)date @@ -118,7 +112,8 @@ - (BOOL)calendar:(FSCalendar *)calendar shouldSelectDate:(NSDate *)date - (void)calendar:(FSCalendar *)calendar didSelectDate:(NSDate *)date { NSLog(@"did select date %@",[calendar stringFromDate:date format:@"yyyy/MM/dd"]); - + CGRect frame = [self.calendar frameForDate:date]; + NSLog(@"%@",NSStringFromCGRect(frame)); } - (void)calendarCurrentPageDidChange:(FSCalendar *)calendar diff --git a/FSCalendar/FSCalendar.h b/FSCalendar/FSCalendar.h index 1dba8e83..71e2e501 100644 --- a/FSCalendar/FSCalendar.h +++ b/FSCalendar/FSCalendar.h @@ -379,13 +379,27 @@ IB_DESIGNABLE - (void)deselectDate:(NSDate *)date; /** - * Change the current page of the calendar. + * Changes the current page of the calendar. * * @param currentPage Representing weekOfYear in week mode, or month in month mode. * @param animated YES if you want to animate the change in position; NO if it should be immediate. */ - (void)setCurrentPage:(NSDate *)currentPage animated:(BOOL)animated; +/** + * Returns the frame for a non-placeholder cell relative to the super view of the calendar. + * + * @param date A date is the calendar. + */ +- (CGRect)frameForDate:(NSDate *)date; + +/** + * Returns the midpoint for a non-placeholder cell relative to the super view of the calendar. + * + * @param date A date is the calendar. + */ +- (CGPoint)centerForDate:(NSDate *)date; + @end #pragma mark - DateTools diff --git a/FSCalendar/FSCalendar.m b/FSCalendar/FSCalendar.m index ea280e41..7505e4c3 100644 --- a/FSCalendar/FSCalendar.m +++ b/FSCalendar/FSCalendar.m @@ -810,6 +810,25 @@ - (void)setCurrentPage:(NSDate *)currentPage animated:(BOOL)animated } } +- (CGRect)frameForDate:(NSDate *)date +{ + if (!self.superview) { + return CGRectZero; + } + CGRect frame = [_collectionViewLayout layoutAttributesForItemAtIndexPath:[self indexPathForDate:date]].frame; + frame = [self.superview convertRect:frame fromView:_collectionView]; + return frame; +} + +- (CGPoint)centerForDate:(NSDate *)date +{ + CGRect frame = [self frameForDate:date]; + if (CGRectIsEmpty(frame)) { + return CGPointZero; + } + return CGPointMake(CGRectGetMidX(frame), CGRectGetMidY(frame)); +} + - (void)setHeaderHeight:(CGFloat)headerHeight { if (_headerHeight != headerHeight) {