From b9e40b15fdf4e309dbb4e8ab848123c513f8e63f Mon Sep 17 00:00:00 2001 From: Wenchao Ding Date: Mon, 25 Jul 2016 10:37:06 +0800 Subject: [PATCH] Better scope transition Better scope transition --- FSCalendar/FSCalendar+DateTools.m | 25 +++++--- FSCalendar/FSCalendar.h | 5 ++ FSCalendar/FSCalendar.m | 2 +- FSCalendar/FSCalendarAnimator.m | 96 +++++++++++-------------------- FSCalendar/FSCalendarCell.m | 2 +- 5 files changed, 60 insertions(+), 70 deletions(-) diff --git a/FSCalendar/FSCalendar+DateTools.m b/FSCalendar/FSCalendar+DateTools.m index 47e329a..537439e 100644 --- a/FSCalendar/FSCalendar+DateTools.m +++ b/FSCalendar/FSCalendar+DateTools.m @@ -104,16 +104,27 @@ - (NSDate *)beginingOfWeekOfDate:(NSDate *)date { NSDateComponents *weekdayComponents = [self.calendar components:NSCalendarUnitWeekday fromDate:date]; - NSDateComponents *componentsToSubtract = self.components; - componentsToSubtract.day = - (weekdayComponents.weekday - self.calendar.firstWeekday); - componentsToSubtract.day = (componentsToSubtract.day-7) % 7; - NSDate *beginningOfWeek = [self.calendar dateByAddingComponents:componentsToSubtract toDate:date options:0]; - NSDateComponents *components = [self.calendar components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay|NSCalendarUnitHour fromDate:beginningOfWeek]; - beginningOfWeek = [self.calendar dateFromComponents:components]; - componentsToSubtract.day = NSIntegerMax; + NSDateComponents *components = self.components; + components.day = - (weekdayComponents.weekday - self.calendar.firstWeekday); + components.day = (components.day-7) % 7; + NSDate *beginningOfWeek = [self.calendar dateByAddingComponents:components toDate:date options:0]; + beginningOfWeek = [self dateByIgnoringTimeComponentsOfDate:beginningOfWeek]; + components.day = NSIntegerMax; return beginningOfWeek; } +- (NSDate *)endOfWeekOfDate:(NSDate *)date +{ + NSDateComponents *weekdayComponents = [self.calendar components:NSCalendarUnitWeekday fromDate:date]; + NSDateComponents *components = self.components; + components.day = - (weekdayComponents.weekday - self.calendar.firstWeekday); + components.day = (components.day-7) % 7 + 6; + NSDate *endOfWeek = [self.calendar dateByAddingComponents:components toDate:date options:0]; + endOfWeek = [self dateByIgnoringTimeComponentsOfDate:endOfWeek]; + components.day = NSIntegerMax; + return endOfWeek; +} + - (NSDate *)middleOfWeekFromDate:(NSDate *)date { NSDateComponents *weekdayComponents = [self.calendar components:NSCalendarUnitWeekday fromDate:date]; diff --git a/FSCalendar/FSCalendar.h b/FSCalendar/FSCalendar.h index 983d7fa..2728b29 100644 --- a/FSCalendar/FSCalendar.h +++ b/FSCalendar/FSCalendar.h @@ -541,6 +541,11 @@ IB_DESIGNABLE */ - (NSDate *)beginingOfWeekOfDate:(NSDate *)date; +/** + * Returns the last day of week of the given date + */ +- (NSDate *)endOfWeekOfDate:(NSDate *)date; + /** * Returns the middle day of week of the given date */ diff --git a/FSCalendar/FSCalendar.m b/FSCalendar/FSCalendar.m index bb569d1..c732c10 100644 --- a/FSCalendar/FSCalendar.m +++ b/FSCalendar/FSCalendar.m @@ -1604,7 +1604,7 @@ typedef NS_ENUM(NSUInteger, FSCalendarOrientation) { if (cell.subtitle) { cell.preferredSubtitleDefaultColor = [self preferredSubtitleDefaultColorForDate:cell.date]; cell.preferredSubtitleSelectionColor = [self preferredSubtitleSelectionColorForDate:cell.date]; - cell.preferredSubtitleOffset = [self preferredTitleOffsetForDate:cell.date]; + cell.preferredSubtitleOffset = [self preferredSubtitleOffsetForDate:cell.date]; } if (cell.numberOfEvents) { cell.preferredEventDefaultColors = [self preferredEventDefaultColorForDate:cell.date]; diff --git a/FSCalendar/FSCalendarAnimator.m b/FSCalendar/FSCalendarAnimator.m index cf6c89d..3654400 100644 --- a/FSCalendar/FSCalendarAnimator.m +++ b/FSCalendar/FSCalendarAnimator.m @@ -404,45 +404,40 @@ if (self.calendar.focusOnSingleSelectedDate) { - NSInteger focusedRowNumber = 0; - NSDate *focusedDate = self.calendar.selectedDate; + __block NSInteger focusedRowNumber = 0; + __block NSDate *focusedDate = self.calendar.selectedDate; - if (focusedDate) { - UICollectionViewLayoutAttributes *itemAttributes = [self.collectionViewLayout layoutAttributesForItemAtIndexPath:[self.calendar indexPathForDate:focusedDate scope:FSCalendarScopeMonth]]; - CGPoint focuedCenter = itemAttributes.center; - if (CGRectContainsPoint(self.collectionView.bounds, focuedCenter)) { - switch (self.collectionViewLayout.scrollDirection) { - case UICollectionViewScrollDirectionHorizontal: { - focusedRowNumber = itemAttributes.indexPath.item%6; - break; - } - case UICollectionViewScrollDirectionVertical: { - focusedRowNumber = itemAttributes.indexPath.item/7; - break; - } - } - } else { - focusedDate = nil; - } - } +#define kCalculateRowNumber \ + do { \ + UICollectionViewLayoutAttributes *itemAttributes = [self.collectionViewLayout layoutAttributesForItemAtIndexPath:[self.calendar indexPathForDate:focusedDate scope:FSCalendarScopeMonth]]; \ + CGPoint focuedCenter = itemAttributes.center; \ + if (CGRectContainsPoint(self.collectionView.bounds, focuedCenter)) { \ + switch (self.collectionViewLayout.scrollDirection) { \ + case UICollectionViewScrollDirectionHorizontal: { \ + focusedRowNumber = itemAttributes.indexPath.item%6; \ + break; \ + } \ + case UICollectionViewScrollDirectionVertical: { \ + focusedRowNumber = itemAttributes.indexPath.item/7; \ + break; \ + } \ + } \ + } else { \ + focusedDate = nil; \ + } \ + } while(0); + + // Focus selected date + if (focusedDate) kCalculateRowNumber + // Focus today if (!focusedDate) { focusedDate = self.calendar.today; - if (focusedDate) { - UICollectionViewLayoutAttributes *itemAttributes = [self.collectionViewLayout layoutAttributesForItemAtIndexPath:[self.calendar indexPathForDate:focusedDate scope:FSCalendarScopeMonth]]; - CGPoint focuedCenter = itemAttributes.center; - if (CGRectContainsPoint(self.collectionView.bounds, focuedCenter)) { - switch (self.collectionViewLayout.scrollDirection) { - case UICollectionViewScrollDirectionHorizontal: { - focusedRowNumber = itemAttributes.indexPath.item%6; - break; - } - case UICollectionViewScrollDirectionVertical: { - focusedRowNumber = itemAttributes.indexPath.item/7; - break; - } - } - } - } + if (focusedDate) kCalculateRowNumber + } + // Focus begining day of month + if (!focusedDate) { + focusedDate = [self.calendar beginingOfMonthOfDate:self.calendar.currentPage]; + kCalculateRowNumber } NSDate *currentPage = self.calendar.currentPage; @@ -455,7 +450,7 @@ attributes.focusedRowNumber = focusedRowNumber; attributes.focusedDate = focusedDate; attributes.targetPage = currentPage; - +#undef kCalculateRowNumber } break; } @@ -468,30 +463,9 @@ NSInteger focusedRowNumber = 0; NSDate *currentPage = self.calendar.currentPage; - NSDate *firstDayOfMonth = nil; - if (self.calendar.focusOnSingleSelectedDate) { - NSDate *focusedDate = self.calendar.selectedDate; - if (focusedDate) { - UICollectionViewLayoutAttributes *itemAttributes = [self.collectionViewLayout layoutAttributesForItemAtIndexPath:[self.calendar indexPathForDate:focusedDate scope:FSCalendarScopeWeek]]; - CGPoint focuedCenter = itemAttributes.center; - if (CGRectContainsPoint(self.collectionView.bounds, focuedCenter)) { - firstDayOfMonth = [self.calendar beginingOfMonthOfDate:focusedDate]; - } else { - focusedDate = nil; - } - } - if (!focusedDate) { - focusedDate = self.calendar.today; - if (focusedDate) { - UICollectionViewLayoutAttributes *itemAttributes = [self.collectionViewLayout layoutAttributesForItemAtIndexPath:[self.calendar indexPathForDate:focusedDate scope:FSCalendarScopeWeek]]; - CGPoint focuedCenter = itemAttributes.center; - if (CGRectContainsPoint(self.collectionView.bounds, focuedCenter)) { - firstDayOfMonth = [self.calendar beginingOfMonthOfDate:focusedDate]; - } - } - }; - attributes.focusedDate = focusedDate; - } + NSDate *focusedDate = [self.calendar endOfWeekOfDate:currentPage]; + NSDate *firstDayOfMonth = [self.calendar beginingOfMonthOfDate:focusedDate]; + attributes.focusedDate = focusedDate; firstDayOfMonth = firstDayOfMonth ?: [self.calendar beginingOfMonthOfDate:currentPage]; NSInteger numberOfPlaceholdersForPrev = [self.calendar numberOfHeadPlaceholdersForMonth:firstDayOfMonth]; NSDate *firstDateOfPage = [self.calendar dateBySubstractingDays:numberOfPlaceholdersForPrev fromDate:firstDayOfMonth]; diff --git a/FSCalendar/FSCalendarCell.m b/FSCalendar/FSCalendarCell.m index ab78c00..cd76f14 100644 --- a/FSCalendar/FSCalendarCell.m +++ b/FSCalendar/FSCalendarCell.m @@ -198,7 +198,7 @@ ); _subtitleLabel.frame = CGRectMake( self.preferredSubtitleOffset.x, - _titleLabel.fs_bottom - (_titleLabel.fs_height-_titleLabel.font.pointSize)+self.preferredSubtitleOffset.y, + (_titleLabel.fs_bottom-self.preferredTitleOffset.y) - (_titleLabel.fs_height-_titleLabel.font.pointSize)+self.preferredSubtitleOffset.y, self.fs_width, subtitleHeight );