193 lines
6.3 KiB
Objective-C
193 lines
6.3 KiB
Objective-C
//
|
|
// 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
|