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