CalenderView.m 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. //
  2. // CalenderView.m
  3. // YZCCalender
  4. //
  5. // Created by Jason on 2018/1/17.
  6. // Copyright © 2018年 jason. All rights reserved.
  7. //
  8. #import "CalenderCollectionCell.h"
  9. #import "CalenderHeaderView.h"
  10. #import "CalenderModel.h"
  11. #import "CalenderView.h"
  12. #import "CalenderWeekView.h"
  13. #import "NSDate+Extension.h"
  14. #import "UIColor+Extension.h"
  15. @interface CalenderView ()<UICollectionViewDelegate, UICollectionViewDataSource>
  16. @property (nonatomic, strong) UICollectionView *collectionView;
  17. @property (nonatomic, strong) NSMutableArray *dataSource;
  18. @property (nonatomic, strong) CalenderWeekView *weekView;
  19. @property (nonatomic, strong) NSIndexPath *lastIndexPath;
  20. @property (nonatomic, copy) NSString *startDay;
  21. @property (nonatomic, copy) NSString *endDay;
  22. @end
  23. static NSString *const reuseIdentifier = @"collectionViewCell";
  24. static NSString *const headerIdentifier = @"headerIdentifier";
  25. @implementation CalenderView
  26. - (instancetype)initWithFrame:(CGRect)frame startDay:(NSString *)startDay endDay:(NSString *)endDay {
  27. self = [super initWithFrame:frame];
  28. if (self) {
  29. self.startDay = startDay;
  30. self.endDay = endDay;
  31. [self buildSource];
  32. [self addSubview:self.weekView];
  33. [self addSubview:self.collectionView];
  34. }
  35. return self;
  36. }
  37. #pragma mark - 设置数据源
  38. - (void)buildSource {
  39. NSAssert(self.startDay.length && self.endDay.length, @"开始时间和结束时间不能为空");
  40. if (!self.startDay.length || !self.endDay.length) {
  41. self.startDay = [NSDate timeStringWithInterval:[NSDate date].timeIntervalSince1970];
  42. self.endDay = [NSDate timeStringWithInterval:[NSDate date].timeIntervalSince1970];
  43. }
  44. NSArray *startArray = [self.startDay componentsSeparatedByString:@"-"];
  45. NSArray *endArray = [self.endDay componentsSeparatedByString:@"-"];
  46. NSInteger month = ([endArray[0] integerValue] - [startArray[0] integerValue])* 12 + ([endArray[1] integerValue] - [startArray[1] integerValue]) + 1;
  47. for (int i = 0; i < month; i++) {
  48. NSMutableArray *array = [[NSMutableArray alloc]init];
  49. [self.dataSource addObject:array];
  50. }
  51. for (int i = 0; i < month; i++) {
  52. int calcNumberMonth = (int)[NSDate month:self.startDay] + i;
  53. int month = (calcNumberMonth)%12;
  54. NSDateComponents *components = [[NSDateComponents alloc]init];
  55. //获取下个月的年月日信息,并将其转为date
  56. components.month = month ? month : 12;
  57. components.year = [startArray[0] integerValue] + (calcNumberMonth == 12 ? 0 : calcNumberMonth) / 12;
  58. components.day = 1;
  59. NSCalendar *calendar = [NSCalendar currentCalendar];
  60. NSDate *nextDate = [calendar dateFromComponents:components];
  61. //获取该月第一天星期几
  62. NSInteger firstDayInThisMounth = [NSDate firstWeekdayInThisMonth:nextDate];
  63. //该月的有多少天daysInThisMounth
  64. NSInteger daysInThisMounth = [NSDate totaldaysInMonth:nextDate];
  65. NSString *string = [[NSString alloc]init];
  66. for (int j = 0; j < (daysInThisMounth > 29 && (firstDayInThisMounth == 6 || firstDayInThisMounth == 5) ? 42 : 35); j++) {
  67. CalenderModel *model = [[CalenderModel alloc] init];
  68. if (components.year == 2020 && components.month == 12) {
  69. model.year = 2019;
  70. }else{
  71. model.year = components.year;
  72. }
  73. model.month = components.month;
  74. if (j < firstDayInThisMounth || j > daysInThisMounth + firstDayInThisMounth - 1) {
  75. string = @"";
  76. model.day = string;
  77. } else {
  78. string = [NSString stringWithFormat:@"%02ld", (long)(j - firstDayInThisMounth + 1)];
  79. if (components.year == [[NSDate date] year] && components.month == [[NSDate date] month] && (j - firstDayInThisMounth + 1) > [[NSDate date] day]) {
  80. model.isUse = NO;
  81. }else{
  82. model.isUse = YES;
  83. }
  84. model.day = string;
  85. NSString *dateStr = [NSString stringWithFormat:@"%zd-%02zd-%@", model.year, model.month, model.day];
  86. if (self.selectedDate.length) {
  87. if ([NSDate isEqualBetweenWithDate:self.selectedDate toDate:dateStr]) {
  88. model.isSelected = YES;
  89. self.lastIndexPath = [NSIndexPath indexPathForRow:j inSection:i];
  90. }
  91. if ([NSDate isToday:dateStr]) {
  92. model.isToday = YES;
  93. }
  94. } else {
  95. if ([NSDate isToday:dateStr]) {
  96. model.isToday = YES;
  97. model.isSelected = YES;
  98. self.lastIndexPath = [NSIndexPath indexPathForRow:j inSection:i];
  99. }
  100. }
  101. }
  102. [[self.dataSource objectAtIndex:i]addObject:model];
  103. }
  104. }
  105. }
  106. #pragma mark - UICollectionViewDelegate, UICollectionViewDataSource
  107. - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
  108. return self.dataSource.count;
  109. }
  110. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
  111. return [(NSMutableArray *)[self.dataSource objectAtIndex:(self.dataSource.count -1 - section)] count];
  112. }
  113. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
  114. CalenderModel *model = self.dataSource[self.dataSource.count - 1 - indexPath.section][indexPath.item];
  115. if (!model.day.length) {
  116. return;
  117. }
  118. NSString *selectDate = [NSString stringWithFormat:@"%zd-%zd-%@", model.year, model.month, model.day];
  119. if (![self isActivity:selectDate]) {
  120. return;
  121. }
  122. if ([self.delegate respondsToSelector:@selector(calenderView:dateString:)]) {
  123. [self.delegate calenderView:indexPath dateString:selectDate];
  124. }
  125. for (NSInteger i = 0; i < self.dataSource.count; i ++) {
  126. NSArray * array = self.dataSource[i];
  127. for (NSInteger j = 0; j < array.count; j ++) {
  128. CalenderModel *lastModel = array[j];
  129. lastModel.isSelected = NO;
  130. }
  131. }
  132. // if (indexPath == self.lastIndexPath) {
  133. // return;
  134. // }
  135. //
  136. // if (self.lastIndexPath) {
  137. // CalenderModel *lastModel = self.dataSource[self.lastIndexPath.section][self.lastIndexPath.item];
  138. // lastModel.isSelected = !lastModel.isSelected;
  139. // self.dataSource[self.lastIndexPath.section][self.lastIndexPath.item] = lastModel;
  140. // }
  141. //
  142. self.dataSource[self.dataSource.count - 1 - indexPath.section][indexPath.item] = model;
  143. model.isSelected = !model.isSelected;
  144. self.lastIndexPath = indexPath;
  145. [self.collectionView reloadData];
  146. }
  147. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
  148. CalenderCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
  149. CalenderModel *model = self.dataSource[self.dataSource.count - 1 - indexPath.section][indexPath.item];
  150. cell.model = model;
  151. return cell;
  152. }
  153. - (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
  154. if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
  155. CalenderHeaderView *heardView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:headerIdentifier forIndexPath:indexPath];
  156. CalenderModel *model = self.dataSource[self.dataSource.count - 1 - indexPath.section][indexPath.item];
  157. heardView.yearAndMonthLabel.text = [NSString stringWithFormat:self.yearMonthFormat.length ? self.yearMonthFormat : @"%zd年%zd月", model.year, model.month];
  158. NSString *dateStr = [NSString stringWithFormat:@"%zd-%02zd", model.year, model.month];
  159. if ([NSDate isCurrenMonth:dateStr]) {
  160. heardView.yearAndMonthLabel.textColor = [UIColor colorWithHexString:@"#999999"];
  161. } else {
  162. heardView.yearAndMonthLabel.textColor = [UIColor colorWithHexString:@"#999999"];
  163. }
  164. return heardView;
  165. }
  166. return nil;
  167. }
  168. #pragma mark - set
  169. - (void)setActvityColor:(BOOL)actvityColor {
  170. [self.dataSource removeAllObjects];
  171. [self buildSource];
  172. [self.collectionView reloadData];
  173. }
  174. - (void)setWeekBottomLineColor:(UIColor *)weekBottomLineColor {
  175. _weekBottomLineColor = weekBottomLineColor;
  176. self.weekView.weekBottomLineColor = weekBottomLineColor;
  177. }
  178. - (void)setShowWeekBottomLine:(BOOL)showWeekBottomLine {
  179. _showWeekBottomLine = showWeekBottomLine;
  180. self.weekView.showLine = showWeekBottomLine;
  181. }
  182. - (BOOL)isActivity:(NSString *)date {
  183. BOOL activity = NO;
  184. NSTimeInterval startInterval = [NSDate timeIntervalFromDateString:[NSString stringWithFormat:@"%@ 00:00:00", self.startDay]];
  185. NSTimeInterval endInterval = [NSDate timeIntervalFromDateString:[NSString stringWithFormat:@"%@ 00:00:00", self.endDay]];
  186. NSTimeInterval currentInterval = [NSDate timeIntervalFromDateString:[NSString stringWithFormat:@"%@ 00:00:00", date]];
  187. if (currentInterval >= startInterval && currentInterval <= endInterval) {
  188. activity = YES;
  189. }
  190. return activity;
  191. }
  192. #pragma mark - lazy
  193. - (UICollectionView *)collectionView {
  194. if (_collectionView == nil) {
  195. float cellw = self.bounds.size.width/7;
  196. UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
  197. [flowLayout setHeaderReferenceSize:CGSizeMake(self.frame.size.width, 50)];
  198. flowLayout.sectionInset = UIEdgeInsetsMake(0, -1, 0, 0);
  199. flowLayout.minimumInteritemSpacing = -1;
  200. flowLayout.minimumLineSpacing = 0;
  201. flowLayout.itemSize = CGSizeMake(cellw, 50);
  202. CGFloat collectionViewY = CGRectGetMaxY(self.weekView.frame);
  203. CGFloat collectionViewH = self.frame.size.height - collectionViewY;
  204. _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, collectionViewY, self.frame.size.width, collectionViewH) collectionViewLayout:flowLayout];
  205. _collectionView.dataSource = self;
  206. _collectionView.delegate = self;
  207. _collectionView.showsVerticalScrollIndicator = NO;
  208. _collectionView.showsHorizontalScrollIndicator = NO;
  209. [_collectionView registerClass:[CalenderHeaderView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:headerIdentifier];
  210. [_collectionView registerClass:[CalenderCollectionCell class] forCellWithReuseIdentifier:reuseIdentifier];
  211. _collectionView.backgroundColor = [UIColor whiteColor];
  212. }
  213. return _collectionView;
  214. }
  215. - (CalenderWeekView *)weekView {
  216. if (_weekView == nil) {
  217. _weekView = [[CalenderWeekView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, 32)];
  218. _weekView.dataSource = self.weekArray ? self.weekArray : @[@"日", @"一", @"二", @"三", @"四", @"五", @"六"];
  219. _weekView.backgroundColor = RGB(245, 245, 245);
  220. }
  221. return _weekView;
  222. }
  223. - (NSMutableArray *)dataSource {
  224. if (_dataSource == nil) {
  225. _dataSource = [NSMutableArray array];
  226. }
  227. return _dataSource;
  228. }
  229. #pragma mark - 滑到最底部
  230. - (void)scrollTableToFoot:(BOOL)animated
  231. {
  232. NSInteger s = [self.collectionView numberOfSections]; //有多少组
  233. if (s<1) return; //无数据时不执行 要不会crash
  234. NSInteger r = [self.collectionView numberOfItemsInSection:s-1]; //最后一组有多少行
  235. if (r<1) return;
  236. NSIndexPath *ip = [NSIndexPath indexPathForRow:r-1 inSection:s-1]; //取最后一行数据
  237. [self.collectionView scrollToItemAtIndexPath:ip atScrollPosition:UICollectionViewScrollPositionBottom animated:animated]; //滚动到最后一行
  238. }
  239. @end