123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239 |
- //
- // 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 = @"请选择查询时间";
- UIBarButtonItem *item = [[UIBarButtonItem alloc]initWithTitle:@"确定" style:UIBarButtonItemStylePlain target:self action:@selector(rightClick)];
- item.tintColor = defGreen;
- item.enabled = NO;
- self.navigationItem.rightBarButtonItem = item;
- }
- 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, 0, 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.firstWeekday = 1;
- 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.appearance.weekdayTextColor = defGreen;
- calendar.appearance.caseOptions = FSCalendarCaseOptionsWeekdayUsesDefaultCase|FSCalendarCaseOptionsHeaderUsesDefaultCase;
- calendar.appearance.headerDateFormat = @"yyyy年MM月";
-
- 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;
-
-
- }
- - (void)dealloc
- {
- NSLog(@"%s",__FUNCTION__);
- }
- -(void)rightClick{
- [self.navigationController popViewControllerAnimated:YES];
- if (_date2 == nil) {
- _date2 = _date1;
- }
- if (_date1>_date2) {
- NSDate *tmp = _date1;
- _date1 = _date2;
- _date2 = tmp;
- }
- NSString *start = [self.dateFormatter stringFromDate:_date1];
- NSString *end = [self.dateFormatter stringFromDate:_date2];
- NSTimeInterval zz = [_date2 timeIntervalSinceDate:_date1];
- NSString *days = [NSString stringWithFormat:@"%d",(int)zz/(60*60*24)+1];
- if (self.selectCheckDateBlock) {
- self.selectCheckDateBlock(start, end, days);
- }
- }
- #pragma mark - FSCalendarDataSource
- - (NSDate *)minimumDateForCalendar:(FSCalendar *)calendar
- {
- return [self.dateFormatter dateFromString:@"2017-01-01"];
- }
- - (NSDate *)maximumDateForCalendar:(FSCalendar *)calendar
- {
- // return [self.gregorian dateByAddingUnit:NSCalendarUnitMonth value:10 toDate:[NSDate date] options:0];
- return [NSDate date];
- }
- - (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 (!self.navigationItem.rightBarButtonItem.isEnabled) {
- self.navigationItem.rightBarButtonItem.enabled = YES;
- }
-
- 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
|