-
Notifications
You must be signed in to change notification settings - Fork 0
/
KalGridView.m
executable file
·372 lines (300 loc) · 11.3 KB
/
KalGridView.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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
/*
* Copyright (c) 2009 Keith Lazuka
* License: http://www.opensource.org/licenses/mit-license.html
*/
#import <CoreGraphics/CoreGraphics.h>
#import "KalGridView.h"
#import "KalView.h"
#import "KalMonthView.h"
#import "KalTileView.h"
#import "KalLogic.h"
#import "KalPrivate.h"
#define SLIDE_NONE 0
#define SLIDE_UP 1
#define SLIDE_DOWN 2
const CGSize kTileSize = { 46.f, 48.f };
static NSString *kSlideAnimationId = @"KalSwitchMonths";
@interface KalGridView ()
@property (nonatomic, strong) NSMutableArray *rangeTiles;
- (void)swapMonthViews;
@end
@implementation KalGridView
{
BOOL _needRemoveRanges;
}
- (void)setBeginDate:(NSDate *)beginDate
{
KalTileView *preTile = [frontMonthView tileForDate:_beginDate];
// preTile.state = KalTileStateNone;
if (preTile.state == KalTileStateSelected) {
preTile.state = KalTileStateNone;
}
else{
preTile.state = KalTileStateSelected;
}
_beginDate = beginDate;
NSLog(@"------_beginDate-----%@",_beginDate);
KalTileView *currentTile = [frontMonthView tileForDate:_beginDate];
if (currentTile.state == KalTileStateSelected) {
currentTile.state = KalTileStateNone;
}
else{
currentTile.state = KalTileStateSelected;
}
// currentTile.state = KalTileStateSelected;
[self removeRanges];
self.endDate = nil;
}
- (void)setEndDate:(NSDate *)endDate
{
KalTileView *beginTile = [frontMonthView tileForDate:self.beginDate];
KalTileView *preTile = [frontMonthView tileForDate:_endDate];
if (preTile.state == KalTileStateSelected) {
preTile.state = KalTileStateNone;
}
else{
preTile.state = KalTileStateSelected;
}
// preTile.state = KalTileStateSelected;
_endDate = endDate;
NSLog(@"------_endDate-----%@",_endDate);
KalTileView *currentTile = [frontMonthView tileForDate:_endDate];
NSDate *realBeginDate;
NSDate *realEndDate;
[self removeRanges];
if (!_endDate || [_endDate isEqualToDate:self.beginDate]) {
return;
} else if ([self.beginDate compare:self.endDate] == NSOrderedAscending) {
realBeginDate = self.beginDate;
realEndDate = self.endDate;
beginTile.state = KalTileStateLeftEnd;
currentTile.state = KalTileStateRightEnd;
} else {
realBeginDate = self.endDate;
realEndDate = self.beginDate;
beginTile.state = KalTileStateRightEnd;
currentTile.state = KalTileStateLeftEnd;
}
int dayCount = [NSDate dayBetweenStartDate:realBeginDate endDate:realEndDate];
for (int i=1; i<dayCount; i++) {
NSDate *nextDay = [realBeginDate offsetDay:i];
KalTileView *nextTile = [frontMonthView tileForDate:nextDay];
if (nextTile) {
nextTile.state = KalTileStateInRange;
[nextTile setNeedsDisplay];
[self.rangeTiles addObject:nextTile];
}
}
}
- (void)removeRanges
{
if (_needRemoveRanges) {
for (KalTileView *tile in self.rangeTiles) {
tile.state = KalTileStateNone;
}
[self.rangeTiles removeAllObjects];
}
}
- (id)initWithFrame:(CGRect)frame logic:(KalLogic *)theLogic delegate:(id<KalViewDelegate>)theDelegate
{
// MobileCal uses 46px wide tiles, with a 2px inner stroke
// along the top and right edges. Since there are 7 columns,
// the width needs to be 46*7 (322px). But the iPhone's screen
// is only 320px wide, so we need to make the
// frame extend just beyond the right edge of the screen
// to accomodate all 7 columns. The 7th day's 2px inner stroke
// will be clipped off the screen, but that's fine because
// MobileCal does the same thing.
frame.size.width = 7 * kTileSize.width;
if (self = [super initWithFrame:frame]) {
_needRemoveRanges = YES;
self.clipsToBounds = YES;
self.backgroundColor = [UIColor whiteColor];
logic = theLogic;
delegate = theDelegate;
CGRect monthRect = CGRectMake(0.f, 0.f, frame.size.width, frame.size.height);
frontMonthView = [[KalMonthView alloc] initWithFrame:monthRect];
backMonthView = [[KalMonthView alloc] initWithFrame:monthRect];
backMonthView.hidden = YES;
[self addSubview:backMonthView];
[self addSubview:frontMonthView];
self.selectionMode = KalSelectionModeSingle;
_rangeTiles = [[NSMutableArray alloc] init];
[self jumpToSelectedMonth];
}
return self;
}
- (void)sizeToFit
{
self.height = frontMonthView.height;
}
#pragma mark -
#pragma mark Touches
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInView:self];
UIView *hitView = [self hitTest:location withEvent:event];
if (!hitView)
return;
if ([hitView isKindOfClass:[KalTileView class]]) {
KalTileView *tile = (KalTileView*)hitView;
if (tile.type & KalTileTypeDisable)
return;
NSDate *date = tile.date;
if ([date isEqualToDate:self.beginDate]) {
date = self.beginDate;
_beginDate = _endDate;
_endDate = date;
} else if ([date isEqualToDate:self.endDate]) {
} else {
self.beginDate = date;
}
}
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
if (self.selectionMode == KalSelectionModeSingle)
return;
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInView:self];
UIView *hitView = [self hitTest:location withEvent:event];
if (!hitView)
return;
if ([hitView isKindOfClass:[KalTileView class]]) {
KalTileView *tile = (KalTileView*)hitView;
if (tile.type & KalTileTypeDisable)
return;
NSDate *endDate = tile.date;
if ([endDate isEqualToDate:self.beginDate] || [endDate isEqualToDate:self.endDate])
return;
if (tile.isFirst || tile.isLast) {
if ([tile.date compare:logic.baseDate] == NSOrderedDescending) {
[delegate showFollowingMonth];
} else {
[delegate showPreviousMonth];
}
}
self.endDate = endDate;
}
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInView:self];
UIView *hitView = [self hitTest:location withEvent:event];
if ([hitView isKindOfClass:[KalTileView class]]) {
KalTileView *tile = (KalTileView*)hitView;
if (tile.type & KalTileTypeDisable)
return;
if ((self.selectionMode == KalSelectionModeSingle && tile.belongsToAdjacentMonth) ||
(self.selectionMode == KalSelectionModeRange && (tile.isFirst || tile.isLast))) {
if ([tile.date compare:logic.baseDate] == NSOrderedDescending) {
[delegate showFollowingMonth];
} else {
[delegate showPreviousMonth];
}
}
if (self.selectionMode == KalSelectionModeRange) {
NSDate *endDate = tile.date;
if ([tile.date isEqualToDate:self.beginDate]) {
if ([[endDate offsetDay:1] compare:self.maxAVailableDate] == NSOrderedDescending) {
endDate = [endDate offsetDay:-1];
} else {
endDate = [endDate offsetDay:0];
}
}
self.endDate = endDate;
NSDate *realBeginDate = self.beginDate;
NSDate *realEndDate = self.endDate;
if ([self.beginDate compare:self.endDate] == NSOrderedDescending) {
realBeginDate = self.endDate;
realEndDate = self.beginDate;
}
if ([(id)delegate respondsToSelector:@selector(didSelectBeginDate:endDate:)]) {
[delegate didSelectBeginDate:realBeginDate endDate:realEndDate];
}
} else {
if ([(id)delegate respondsToSelector:@selector(didSelectDate:)]) {
[delegate didSelectDate:self.beginDate];
}
}
}
}
#pragma mark -
#pragma mark Slide Animation
- (void)swapMonthsAndSlide:(int)direction keepOneRow:(BOOL)keepOneRow
{
backMonthView.hidden = NO;
// set initial positions before the slide
if (direction == SLIDE_UP) {
backMonthView.top = keepOneRow
? frontMonthView.bottom - kTileSize.height
: frontMonthView.bottom;
} else if (direction == SLIDE_DOWN) {
NSUInteger numWeeksToKeep = keepOneRow ? 1 : 0;
NSInteger numWeeksToSlide = [backMonthView numWeeks] - numWeeksToKeep;
backMonthView.top = -numWeeksToSlide * kTileSize.height;
} else {
backMonthView.top = 0.f;
}
// trigger the slide animation
[UIView beginAnimations:kSlideAnimationId context:NULL]; {
[UIView setAnimationsEnabled:direction!=SLIDE_NONE];
[UIView setAnimationDuration:0.5];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(animationDidStop:finished:context:)];
frontMonthView.top = -backMonthView.top;
backMonthView.top = 0.f;
frontMonthView.alpha = 0.f;
backMonthView.alpha = 1.f;
self.height = backMonthView.height;
[self swapMonthViews];
} [UIView commitAnimations];
[UIView setAnimationsEnabled:YES];
}
- (void)slide:(int)direction
{
self.transitioning = YES;
[backMonthView showDates:logic.daysInSelectedMonth
leadingAdjacentDates:logic.daysInFinalWeekOfPreviousMonth
trailingAdjacentDates:logic.daysInFirstWeekOfFollowingMonth
minAvailableDate:self.minAvailableDate
maxAvailableDate:self.maxAVailableDate];
// At this point, the calendar logic has already been advanced or retreated to the
// following/previous month, so in order to determine whether there are
// any cells to keep, we need to check for a partial week in the month
// that is sliding offscreen.
BOOL keepOneRow = (direction == SLIDE_UP && [logic.daysInFinalWeekOfPreviousMonth count] > 0)
|| (direction == SLIDE_DOWN && [logic.daysInFirstWeekOfFollowingMonth count] > 0);
[self swapMonthsAndSlide:direction keepOneRow:keepOneRow];
if (self.selectionMode == KalSelectionModeSingle) {
self.beginDate = _beginDate;
} else {
_needRemoveRanges = NO;
self.endDate = _endDate;
_needRemoveRanges = YES;
}
}
- (void)slideUp { [self slide:SLIDE_UP]; }
- (void)slideDown { [self slide:SLIDE_DOWN]; }
- (void)animationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context
{
self.transitioning = NO;
backMonthView.hidden = YES;
}
#pragma mark -
- (void)swapMonthViews
{
KalMonthView *tmp = backMonthView;
backMonthView = frontMonthView;
frontMonthView = tmp;
[self exchangeSubviewAtIndex:[self.subviews indexOfObject:frontMonthView] withSubviewAtIndex:[self.subviews indexOfObject:backMonthView]];
}
- (void)jumpToSelectedMonth
{
[self slide:SLIDE_NONE];
}
- (void)markTilesForDates:(NSArray *)dates { [frontMonthView markTilesForDates:dates]; }
#pragma mark -
@end