Better scope transition

Better scope transition
This commit is contained in:
Wenchao Ding 2016-07-25 10:37:06 +08:00
parent 8bd92bf508
commit b9e40b15fd
5 changed files with 60 additions and 70 deletions

View File

@ -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];

View File

@ -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
*/

View File

@ -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];

View File

@ -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];

View File

@ -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
);