做一个自己的日历
思路:整体用collectionView
难点:组织日期数据,分组背景效果,组头悬停效果
1.组织日期数据
1.两层for循环,创建一个二维数组,第一层为月份,第二层为每个月下的天数。
2.计算起始日期之间一共有几个月,用于第一层数组。
3.计算各个月份一共有几天。并计算每月第一天是星期几,用来添加对应数量的空数据模型。对应天的model对象添加到每月的数组,构成第二层数组。
#pragma mark - 数据源方法
- (NSMutableArray *)setUpDataStartDate:(NSDate *)startDate endDate:(NSDate *)endDate {
NSTimeInterval timeInterval = [endDate timeIntervalSinceDate:startDate];
if (timeInterval < 0) {
return nil;
}
NSTimeInterval todayTime = [[NSDate new]timeIntervalSince1970];
// 计算开始结束日期之间相差几个月
NSInteger monthInterval;
NSDateComponents *startComponents = [startDate getDateComponents];
NSDateComponents *endComponents = [endDate getDateComponents];
NSInteger startYear = startComponents.year;
NSInteger endYear = endComponents.year;
NSInteger startMonth = startComponents.month;
NSInteger endMonth = endComponents.month;
monthInterval = (endYear - startYear) * 12 + (endMonth - startMonth) + 1;
NSMutableArray *dataArray = [NSMutableArray new];
for (int i = 0; i < monthInterval; i++) {
NSMutableArray *monthArray = [NSMutableArray array];
NSDateComponents *newComponent = [NSDateComponents new];
NSInteger nowYear = startYear + (startMonth + i)/12;
newComponent.year = nowYear;
NSInteger monthYu = (startMonth + i) % 12;
NSInteger nowMonth = monthYu ? monthYu : 12;
newComponent.month = nowMonth;
newComponent.day = 1;
NSDate *newDate = [[NSCalendar currentCalendar]dateFromComponents:newComponent];
// 本月第一天是周几
NSInteger weekFirstDay = [newDate getFirstWeekInMonth];
// 再本月第一天之前添加空的数据模型
if (weekFirstDay == 7) {
weekFirstDay = 0;
}
for (int j = 0; j < weekFirstDay; j++) {
CalendarModel *model = [CalendarModel new];
model.dateStatus = Calendar_EmptyDate;
[monthArray addObject:model];
}
// 添加有数据的数据模型
NSInteger daysMonth = [newDate getDayNumOfMouth];
for (int k = 1; k <= daysMonth ; k++) {
CalendarModel *model = [CalendarModel new];
model.year = nowYear;
model.month = nowMonth;
model.day = k;
// 计算周几
newComponent.day = k;
NSDate *weekDate = [[NSCalendar currentCalendar]dateFromComponents:newComponent];
model.week = [weekDate getWeek];
NSTimeInterval modelTimeStamp = weekDate.timeIntervalSince1970 + 60*60*24 - 1;
if (modelTimeStamp < todayTime) {
model.dateStatus = Calendar_PassedDate;
}else{
model.dateStatus = Calendar_NormalDate;
}
[monthArray addObject:model];
}
[dataArray addObject:monthArray];
}
return dataArray;
}
2.区间选择日期
逻辑:选择开始日期和结束日期区间,开始日期标记为开始变红,结束日期标记为结束变红,两个日期之间的日期变紫。特殊情况:已经选择开始日期,选择结束日期比开始日期早,取消原来的开始日期,将这次点击变为选择开始日期,每次点击选择日期,item的model状态改变重新刷新collectionView
3.添加分组月份阴影效果
1.需要自定义装饰视图,继承UICollectionReusableView实现自定义视图
2.在自定义FlowLayout中注册自定义的装饰视图
- (void)prepareLayout {
[super prepareLayout];
[self registerClass:[DSCollectionReusableView class] forDecorationViewOfKind:kDecorationReuseIdentifier];
}
3.因为每个collectionView的section背景装饰视图的月份是不同的,所以需要动态改变。不能直接传值,只能通过layoutAttributesForElementsInRect方法,添加装饰视图
- (NSArray<UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect {
NSArray *superAttrs = [super layoutAttributesForElementsInRect:rect];
// 避免多次调用设置section背景图片
NSInteger sectionNum = 0;
BOOL isSet = true;
for (UICollectionViewLayoutAttributes *attr in superAttrs) {
if (isSet) {
if (attr.indexPath.section == 0) {
sectionNum = attr.indexPath.section;
}else {
sectionNum = attr.indexPath.section + 1;//只计算当前显示的分组
}
isSet = false;
}
if (sectionNum == attr.indexPath.section) {
[attrs addObject:[self layoutAttributesForDecorationViewOfKind:kDecorationReuseIdentifier atIndexPath:attr.indexPath top:attr.frame.origin.y]];
sectionNum ++;
}
}
return attrs;
}
- (UICollectionViewLayoutAttributes *)layoutAttributesForDecorationViewOfKind:(NSString *)decorationViewKind atIndexPath:(NSIndexPath *)indexPath top:(CGFloat)top {
// UICollectionViewLayoutAttributes *layoutAttribute = [UICollectionViewLayoutAttributes layoutAttributesForDecorationViewOfKind:decorationViewKind withIndexPath:indexPath];
DSLayoutAttributes *layoutAttribute = [DSLayoutAttributes layoutAttributesForDecorationViewOfKind:decorationViewKind withIndexPath:indexPath];
layoutAttribute.mouthStr = [NSString stringWithFormat:@"%@",self.monthArray[indexPath.section]];
layoutAttribute.frame = CGRectMake(0, top, [UIScreen mainScreen].bounds.size.width, 375);
layoutAttribute.zIndex = -1;
return layoutAttribute;
}
4.组头悬停效果
iOS 9.0之后直接设置flowLayout.sectionHeadersPinToVisibleBounds = true;
就可以。但是9.0之前需要自己手动在自定义FlowLayout中计算,组头悬停。可以参考下这老哥的https://www.jianshu.com/p/f0a2a7a81c6f
我的github地址demo:https://github.com/D-james/Calendar