forked from WenchaoD/FSCalendar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FSCalendarScopeExampleViewController.m
192 lines (153 loc) · 6.32 KB
/
FSCalendarScopeExampleViewController.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
//
// FSCalendarScopeExampleViewController.m
// FSCalendar
//
// Created by Wenchao Ding on 8/29/15.
// Copyright (c) 2015 Wenchao Ding. All rights reserved.
//
#import "FSCalendarScopeExampleViewController.h"
NS_ASSUME_NONNULL_BEGIN
@interface FSCalendarScopeExampleViewController()<UITableViewDataSource,UITableViewDelegate,FSCalendarDataSource,FSCalendarDelegate,UIGestureRecognizerDelegate>
{
void * _KVOContext;
}
@property (weak, nonatomic) IBOutlet FSCalendar *calendar;
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (weak, nonatomic) IBOutlet UISwitch *animationSwitch;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *calendarHeightConstraint;
@property (strong, nonatomic) NSDateFormatter *dateFormatter;
@property (strong, nonatomic) UIPanGestureRecognizer *scopeGesture;
- (IBAction)toggleClicked:(id)sender;
@end
NS_ASSUME_NONNULL_END
@implementation FSCalendarScopeExampleViewController
#pragma mark - Life cycle
- (instancetype)initWithCoder:(NSCoder *)coder
{
self = [super initWithCoder:coder];
if (self) {
self.dateFormatter = [[NSDateFormatter alloc] init];
self.dateFormatter.dateFormat = @"yyyy/MM/dd";
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
if ([[UIDevice currentDevice].model hasPrefix:@"iPad"]) {
self.calendarHeightConstraint.constant = 400;
}
UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self.calendar action:@selector(handleScopeGesture:)];
panGesture.delegate = self;
panGesture.minimumNumberOfTouches = 1;
panGesture.maximumNumberOfTouches = 2;
[self.view addGestureRecognizer:panGesture];
self.scopeGesture = panGesture;
// While the scope gesture begin, the pan gesture of tableView should cancel.
[self.tableView.panGestureRecognizer requireGestureRecognizerToFail:panGesture];
[self.calendar addObserver:self forKeyPath:@"scope" options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld context:_KVOContext];
self.calendar.placeholderType = FSCalendarPlaceholderTypeNone;
self.calendar.scope = FSCalendarScopeWeek;
[self.calendar selectDate:[NSDate date] scrollToDate:YES];
// For UITest
self.calendar.accessibilityIdentifier = @"calendar";
}
- (void)dealloc
{
[self.calendar removeObserver:self forKeyPath:@"scope" context:_KVOContext];
NSLog(@"%s",__FUNCTION__);
}
#pragma mark - KVO
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context
{
if (context == _KVOContext) {
FSCalendarScope oldScope = [change[NSKeyValueChangeOldKey] unsignedIntegerValue];
FSCalendarScope newScope = [change[NSKeyValueChangeNewKey] unsignedIntegerValue];
NSLog(@"From %@ to %@",(oldScope==FSCalendarScopeWeek?@"week":@"month"),(newScope==FSCalendarScopeWeek?@"week":@"month"));
} else {
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
}
}
#pragma mark - <UIGestureRecognizerDelegate>
// Whether scope gesture should begin
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
BOOL shouldBegin = self.tableView.contentOffset.y <= -self.tableView.contentInset.top;
if (shouldBegin) {
CGPoint velocity = [self.scopeGesture velocityInView:self.view];
switch (self.calendar.scope) {
case FSCalendarScopeMonth:
return velocity.y < 0;
case FSCalendarScopeWeek:
return velocity.y > 0;
}
}
return shouldBegin;
}
#pragma mark - <FSCalendarDelegate>
- (void)calendar:(FSCalendar *)calendar boundingRectWillChange:(CGRect)bounds animated:(BOOL)animated
{
// NSLog(@"%@",(calendar.scope==FSCalendarScopeWeek?@"week":@"month"));
_calendarHeightConstraint.constant = CGRectGetHeight(bounds);
[self.view layoutIfNeeded];
}
- (void)calendar:(FSCalendar *)calendar didSelectDate:(NSDate *)date atMonthPosition:(FSCalendarMonthPosition)monthPosition
{
NSLog(@"did select date %@",[self.dateFormatter stringFromDate:date]);
NSMutableArray *selectedDates = [NSMutableArray arrayWithCapacity:calendar.selectedDates.count];
[calendar.selectedDates enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
[selectedDates addObject:[self.dateFormatter stringFromDate:obj]];
}];
NSLog(@"selected dates is %@",selectedDates);
if (monthPosition == FSCalendarMonthPositionNext || monthPosition == FSCalendarMonthPositionPrevious) {
[calendar setCurrentPage:date animated:YES];
}
}
- (void)calendarCurrentPageDidChange:(FSCalendar *)calendar
{
NSLog(@"%s %@", __FUNCTION__, [self.dateFormatter stringFromDate:calendar.currentPage]);
}
#pragma mark - <UITableViewDataSource>
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSInteger numbers[2] = {2,20};
return numbers[section];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.section == 0) {
NSString *identifier = @[@"cell_month",@"cell_week"][indexPath.row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
return cell;
} else {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
return cell;
}
}
#pragma mark - <UITableViewDelegate>
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
if (indexPath.section == 0) {
FSCalendarScope selectedScope = indexPath.row == 0 ? FSCalendarScopeMonth : FSCalendarScopeWeek;
[self.calendar setScope:selectedScope animated:self.animationSwitch.on];
}
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 20;
}
#pragma mark - Target actions
- (IBAction)toggleClicked:(id)sender
{
if (self.calendar.scope == FSCalendarScopeMonth) {
[self.calendar setScope:FSCalendarScopeWeek animated:_animationSwitch.on];
} else {
[self.calendar setScope:FSCalendarScopeMonth animated:_animationSwitch.on];
}
}
@end