-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJTCalendarDayView.m
executable file
·324 lines (260 loc) · 9.15 KB
/
JTCalendarDayView.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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
//
// JTCalendarDayView.m
// JTCalendar
//
// Created by Jonathan Tribouharet
//
#import "JTCalendarDayView.h"
#import "JTCircleView.h"
@interface JTCalendarDayView (){
UIView *backgroundView;
JTCircleView *circleView;
UILabel *textLabel;
JTCircleView *dotView;
BOOL isSelected;
int cacheIsToday;
NSString *cacheCurrentDateText;
}
@end
static NSString *const kJTCalendarDaySelected = @"kJTCalendarDaySelected";
@implementation JTCalendarDayView
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if(!self){
return nil;
}
[self commonInit];
return self;
}
- (id)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
if(!self){
return nil;
}
[self commonInit];
return self;
}
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)commonInit
{
isSelected = NO;
self.isOtherMonth = NO;
{
backgroundView = [UIView new];
[self addSubview:backgroundView];
}
{
circleView = [JTCircleView new];
[self addSubview:circleView];
}
{
textLabel = [UILabel new];
[self addSubview:textLabel];
}
{
dotView = [JTCircleView new];
[self addSubview:dotView];
dotView.hidden = YES;
}
{
UITapGestureRecognizer *gesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(didTouch)];
self.userInteractionEnabled = YES;
[self addGestureRecognizer:gesture];
}
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didDaySelected:) name:kJTCalendarDaySelected object:nil];
}
}
- (void)layoutSubviews
{
[self configureConstraintsForSubviews];
// No need to call [super layoutSubviews]
}
// Avoid to calcul constraints (very expensive)
- (void)configureConstraintsForSubviews
{
textLabel.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
backgroundView.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
CGFloat sizeCircle = MIN(self.frame.size.width, self.frame.size.height);
CGFloat sizeDot = sizeCircle;
sizeCircle = sizeCircle * self.calendarManager.calendarAppearance.dayCircleRatio;
sizeDot = sizeDot * self.calendarManager.calendarAppearance.dayDotRatio;
sizeCircle = roundf(sizeCircle);
sizeDot = roundf(sizeDot);
circleView.frame = CGRectMake(0, 0, sizeCircle, sizeCircle);
circleView.center = CGPointMake(self.frame.size.width / 2., self.frame.size.height / 2.);
circleView.layer.cornerRadius = sizeCircle / 2.;
dotView.frame = CGRectMake(0, 0, sizeDot, sizeDot);
dotView.center = CGPointMake(self.frame.size.width / 2., (self.frame.size.height / 2.) + sizeDot * 2.5);
dotView.layer.cornerRadius = sizeDot / 2.;
}
- (void)setDate:(NSDate *)date
{
static NSDateFormatter *dateFormatter;
if(!dateFormatter){
dateFormatter = [NSDateFormatter new];
dateFormatter.timeZone = self.calendarManager.calendarAppearance.calendar.timeZone;
[dateFormatter setDateFormat:self.calendarManager.calendarAppearance.dayFormat];
}
self->_date = date;
textLabel.text = [dateFormatter stringFromDate:date];
cacheIsToday = -1;
cacheCurrentDateText = nil;
}
- (void)didTouch
{
[self setSelected:YES animated:YES];
[self.calendarManager setCurrentDateSelected:self.date];
[[NSNotificationCenter defaultCenter] postNotificationName:kJTCalendarDaySelected object:self.date];
[self.calendarManager.dataSource calendarDidDateSelected:self.calendarManager date:self.date];
if(!self.isOtherMonth || !self.calendarManager.calendarAppearance.autoChangeMonth){
return;
}
NSInteger currentMonthIndex = [self monthIndexForDate:self.date];
NSInteger calendarMonthIndex = [self monthIndexForDate:self.calendarManager.currentDate];
currentMonthIndex = currentMonthIndex % 12;
if(currentMonthIndex == (calendarMonthIndex + 1) % 12){
[self.calendarManager loadNextPage];
}
else if(currentMonthIndex == (calendarMonthIndex + 12 - 1) % 12){
[self.calendarManager loadPreviousPage];
}
}
- (void)didDaySelected:(NSNotification *)notification
{
NSDate *dateSelected = [notification object];
if([self isSameDate:dateSelected]){
if(!isSelected){
[self setSelected:YES animated:YES];
}
}
else if(isSelected){
[self setSelected:NO animated:YES];
}
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
if(isSelected == selected){
animated = NO;
}
isSelected = selected;
circleView.transform = CGAffineTransformIdentity;
CGAffineTransform tr = CGAffineTransformIdentity;
CGFloat opacity = 1.;
if(selected){
if(!self.isOtherMonth){
circleView.color = [self.calendarManager.calendarAppearance dayCircleColorSelected];
textLabel.textColor = [self.calendarManager.calendarAppearance dayTextColorSelected];
dotView.color = [self.calendarManager.calendarAppearance dayDotColorSelected];
}
else{
circleView.color = [self.calendarManager.calendarAppearance dayCircleColorSelectedOtherMonth];
textLabel.textColor = [self.calendarManager.calendarAppearance dayTextColorSelectedOtherMonth];
dotView.color = [self.calendarManager.calendarAppearance dayDotColorSelectedOtherMonth];
}
circleView.transform = CGAffineTransformScale(CGAffineTransformIdentity, 0.1, 0.1);
tr = CGAffineTransformIdentity;
}
else if([self isToday]){
if(!self.isOtherMonth){
circleView.color = [self.calendarManager.calendarAppearance dayCircleColorToday];
textLabel.textColor = [self.calendarManager.calendarAppearance dayTextColorToday];
dotView.color = [self.calendarManager.calendarAppearance dayDotColorToday];
}
else{
circleView.color = [self.calendarManager.calendarAppearance dayCircleColorTodayOtherMonth];
textLabel.textColor = [self.calendarManager.calendarAppearance dayTextColorTodayOtherMonth];
dotView.color = [self.calendarManager.calendarAppearance dayDotColorTodayOtherMonth];
}
}
else{
if(!self.isOtherMonth){
textLabel.textColor = [self.calendarManager.calendarAppearance dayTextColor];
dotView.color = [self.calendarManager.calendarAppearance dayDotColor];
}
else{
textLabel.textColor = [self.calendarManager.calendarAppearance dayTextColorOtherMonth];
dotView.color = [self.calendarManager.calendarAppearance dayDotColorOtherMonth];
}
opacity = 0.;
}
if(animated){
[UIView animateWithDuration:.3 animations:^{
circleView.layer.opacity = opacity;
circleView.transform = tr;
}];
}
else{
circleView.layer.opacity = opacity;
circleView.transform = tr;
}
}
- (void)setIsOtherMonth:(BOOL)isOtherMonth
{
self->_isOtherMonth = isOtherMonth;
[self setSelected:isSelected animated:NO];
}
- (void)reloadData
{
dotView.hidden = ![self.calendarManager.dataCache haveEvent:self.date];
BOOL selected = [self isSameDate:[self.calendarManager currentDateSelected]];
[self setSelected:selected animated:NO];
}
- (BOOL)isToday
{
if(cacheIsToday == 0){
return NO;
}
else if(cacheIsToday == 1){
return YES;
}
else{
if([self isSameDate:[NSDate date]]){
cacheIsToday = 1;
return YES;
}
else{
cacheIsToday = 0;
return NO;
}
}
}
- (BOOL)isSameDate:(NSDate *)date
{
static NSDateFormatter *dateFormatter;
if(!dateFormatter){
dateFormatter = [NSDateFormatter new];
dateFormatter.timeZone = self.calendarManager.calendarAppearance.calendar.timeZone;
[dateFormatter setDateFormat:@"dd-MM-yyyy"];
}
if(!cacheCurrentDateText){
cacheCurrentDateText = [dateFormatter stringFromDate:self.date];
}
NSString *dateText2 = [dateFormatter stringFromDate:date];
if ([cacheCurrentDateText isEqualToString:dateText2]) {
return YES;
}
return NO;
}
- (NSInteger)monthIndexForDate:(NSDate *)date
{
NSCalendar *calendar = self.calendarManager.calendarAppearance.calendar;
NSDateComponents *comps = [calendar components:NSCalendarUnitMonth fromDate:date];
return comps.month;
}
- (void)reloadAppearance
{
textLabel.textAlignment = NSTextAlignmentCenter;
textLabel.font = self.calendarManager.calendarAppearance.dayTextFont;
backgroundView.backgroundColor = self.calendarManager.calendarAppearance.dayBackgroundColor;
backgroundView.layer.borderWidth = self.calendarManager.calendarAppearance.dayBorderWidth;
backgroundView.layer.borderColor = self.calendarManager.calendarAppearance.dayBorderColor.CGColor;
[self configureConstraintsForSubviews];
[self setSelected:isSelected animated:NO];
}
@end