RangePickerViewController.m 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. //
  2. // RangePickerViewController.m
  3. // FSCalendar
  4. //
  5. // Created by dingwenchao on 5/8/16.
  6. // Copyright © 2016 Wenchao Ding. All rights reserved.
  7. //
  8. #import "RangePickerViewController.h"
  9. #import "FSCalendar.h"
  10. #import "RangePickerCell.h"
  11. #import "FSCalendarExtensions.h"
  12. @interface RangePickerViewController () <FSCalendarDataSource,FSCalendarDelegate,FSCalendarDelegateAppearance>
  13. @property (weak, nonatomic) FSCalendar *calendar;
  14. @property (weak, nonatomic) UILabel *eventLabel;
  15. @property (strong, nonatomic) NSCalendar *gregorian;
  16. @property (strong, nonatomic) NSDateFormatter *dateFormatter;
  17. // The start date of the range
  18. @property (strong, nonatomic) NSDate *date1;
  19. // The end date of the range
  20. @property (strong, nonatomic) NSDate *date2;
  21. - (void)configureCell:(__kindof FSCalendarCell *)cell forDate:(NSDate *)date atMonthPosition:(FSCalendarMonthPosition)position;
  22. @end
  23. @implementation RangePickerViewController
  24. - (instancetype)init
  25. {
  26. self = [super init];
  27. if (self) {
  28. self.title = @"请选择查询时间";
  29. UIBarButtonItem *item = [[UIBarButtonItem alloc]initWithTitle:@"确定" style:UIBarButtonItemStylePlain target:self action:@selector(rightClick)];
  30. item.tintColor = defGreen;
  31. item.enabled = NO;
  32. self.navigationItem.rightBarButtonItem = item;
  33. }
  34. return self;
  35. }
  36. - (void)loadView
  37. {
  38. UIView *view = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
  39. view.backgroundColor = [UIColor whiteColor];
  40. self.view = view;
  41. FSCalendar *calendar = [[FSCalendar alloc] initWithFrame:CGRectMake(0, 0, view.frame.size.width, view.frame.size.height - CGRectGetMaxY(self.navigationController.navigationBar.frame))];
  42. calendar.dataSource = self;
  43. calendar.delegate = self;
  44. calendar.pagingEnabled = NO;
  45. calendar.allowsMultipleSelection = YES;
  46. calendar.rowHeight = 60;
  47. calendar.firstWeekday = 1;
  48. calendar.placeholderType = FSCalendarPlaceholderTypeNone;
  49. [view addSubview:calendar];
  50. self.calendar = calendar;
  51. calendar.appearance.titleDefaultColor = [UIColor blackColor];
  52. calendar.appearance.headerTitleColor = [UIColor blackColor];
  53. calendar.appearance.titleFont = [UIFont systemFontOfSize:16];
  54. calendar.appearance.weekdayTextColor = defGreen;
  55. calendar.appearance.caseOptions = FSCalendarCaseOptionsWeekdayUsesDefaultCase|FSCalendarCaseOptionsHeaderUsesDefaultCase;
  56. calendar.appearance.headerDateFormat = @"yyyy年MM月";
  57. calendar.swipeToChooseGesture.enabled = YES;
  58. calendar.today = nil; // Hide the today circle
  59. [calendar registerClass:[RangePickerCell class] forCellReuseIdentifier:@"cell"];
  60. }
  61. - (void)viewDidLoad
  62. {
  63. [super viewDidLoad];
  64. self.gregorian = [NSCalendar calendarWithIdentifier:NSCalendarIdentifierGregorian];
  65. self.dateFormatter = [[NSDateFormatter alloc] init];
  66. self.dateFormatter.dateFormat = @"yyyy-MM-dd";
  67. // Uncomment this to perform an 'initial-week-scope'
  68. // self.calendar.scope = FSCalendarScopeWeek;
  69. }
  70. - (void)dealloc
  71. {
  72. NSLog(@"%s",__FUNCTION__);
  73. }
  74. -(void)rightClick{
  75. [self.navigationController popViewControllerAnimated:YES];
  76. if (_date2 == nil) {
  77. _date2 = _date1;
  78. }
  79. if (_date1>_date2) {
  80. NSDate *tmp = _date1;
  81. _date1 = _date2;
  82. _date2 = tmp;
  83. }
  84. NSString *start = [self.dateFormatter stringFromDate:_date1];
  85. NSString *end = [self.dateFormatter stringFromDate:_date2];
  86. NSTimeInterval zz = [_date2 timeIntervalSinceDate:_date1];
  87. NSString *days = [NSString stringWithFormat:@"%d",(int)zz/(60*60*24)+1];
  88. if (self.selectCheckDateBlock) {
  89. self.selectCheckDateBlock(start, end, days);
  90. }
  91. }
  92. #pragma mark - FSCalendarDataSource
  93. - (NSDate *)minimumDateForCalendar:(FSCalendar *)calendar
  94. {
  95. return [self.dateFormatter dateFromString:@"2017-01-01"];
  96. }
  97. - (NSDate *)maximumDateForCalendar:(FSCalendar *)calendar
  98. {
  99. // return [self.gregorian dateByAddingUnit:NSCalendarUnitMonth value:10 toDate:[NSDate date] options:0];
  100. return [NSDate date];
  101. }
  102. - (NSString *)calendar:(FSCalendar *)calendar titleForDate:(NSDate *)date
  103. {
  104. if ([self.gregorian isDateInToday:date]) {
  105. return @"今";
  106. }
  107. return nil;
  108. }
  109. - (FSCalendarCell *)calendar:(FSCalendar *)calendar cellForDate:(NSDate *)date atMonthPosition:(FSCalendarMonthPosition)monthPosition
  110. {
  111. RangePickerCell *cell = [calendar dequeueReusableCellWithIdentifier:@"cell" forDate:date atMonthPosition:monthPosition];
  112. return cell;
  113. }
  114. - (void)calendar:(FSCalendar *)calendar willDisplayCell:(FSCalendarCell *)cell forDate:(NSDate *)date atMonthPosition: (FSCalendarMonthPosition)monthPosition
  115. {
  116. [self configureCell:cell forDate:date atMonthPosition:monthPosition];
  117. }
  118. #pragma mark - FSCalendarDelegate
  119. - (BOOL)calendar:(FSCalendar *)calendar shouldSelectDate:(NSDate *)date atMonthPosition:(FSCalendarMonthPosition)monthPosition
  120. {
  121. return monthPosition == FSCalendarMonthPositionCurrent;
  122. }
  123. - (BOOL)calendar:(FSCalendar *)calendar shouldDeselectDate:(NSDate *)date atMonthPosition:(FSCalendarMonthPosition)monthPosition
  124. {
  125. return NO;
  126. }
  127. - (void)calendar:(FSCalendar *)calendar didSelectDate:(NSDate *)date atMonthPosition:(FSCalendarMonthPosition)monthPosition
  128. {
  129. if (!self.navigationItem.rightBarButtonItem.isEnabled) {
  130. self.navigationItem.rightBarButtonItem.enabled = YES;
  131. }
  132. if (calendar.swipeToChooseGesture.state == UIGestureRecognizerStateChanged) {
  133. // If the selection is caused by swipe gestures
  134. if (!self.date1) {
  135. self.date1 = date;
  136. } else {
  137. if (self.date2) {
  138. [calendar deselectDate:self.date2];
  139. }
  140. self.date2 = date;
  141. }
  142. } else {
  143. if (self.date2) {
  144. [calendar deselectDate:self.date1];
  145. [calendar deselectDate:self.date2];
  146. self.date1 = date;
  147. self.date2 = nil;
  148. } else if (!self.date1) {
  149. self.date1 = date;
  150. } else {
  151. self.date2 = date;
  152. }
  153. }
  154. [self configureVisibleCells];
  155. }
  156. - (void)calendar:(FSCalendar *)calendar didDeselectDate:(NSDate *)date atMonthPosition:(FSCalendarMonthPosition)monthPosition
  157. {
  158. NSLog(@"did deselect date %@",[self.dateFormatter stringFromDate:date]);
  159. [self configureVisibleCells];
  160. }
  161. - (NSArray<UIColor *> *)calendar:(FSCalendar *)calendar appearance:(FSCalendarAppearance *)appearance eventDefaultColorsForDate:(NSDate *)date
  162. {
  163. if ([self.gregorian isDateInToday:date]) {
  164. return @[[UIColor orangeColor]];
  165. }
  166. return @[appearance.eventDefaultColor];
  167. }
  168. #pragma mark - Private methods
  169. - (void)configureVisibleCells
  170. {
  171. [self.calendar.visibleCells enumerateObjectsUsingBlock:^(__kindof FSCalendarCell * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
  172. NSDate *date = [self.calendar dateForCell:obj];
  173. FSCalendarMonthPosition position = [self.calendar monthPositionForCell:obj];
  174. [self configureCell:obj forDate:date atMonthPosition:position];
  175. }];
  176. }
  177. - (void)configureCell:(__kindof FSCalendarCell *)cell forDate:(NSDate *)date atMonthPosition:(FSCalendarMonthPosition)position
  178. {
  179. RangePickerCell *rangeCell = cell;
  180. if (position != FSCalendarMonthPositionCurrent) {
  181. rangeCell.middleLayer.hidden = YES;
  182. rangeCell.selectionLayer.hidden = YES;
  183. return;
  184. }
  185. if (self.date1 && self.date2) {
  186. // The date is in the middle of the range
  187. BOOL isMiddle = [date compare:self.date1] != [date compare:self.date2];
  188. rangeCell.middleLayer.hidden = !isMiddle;
  189. } else {
  190. rangeCell.middleLayer.hidden = YES;
  191. }
  192. BOOL isSelected = NO;
  193. isSelected |= self.date1 && [self.gregorian isDate:date inSameDayAsDate:self.date1];
  194. isSelected |= self.date2 && [self.gregorian isDate:date inSameDayAsDate:self.date2];
  195. rangeCell.selectionLayer.hidden = !isSelected;
  196. }
  197. @end