? ? ?FSCalendar是開源iOS日歷控件而咆,支持橫向霍比、縱向滑動(dòng). 月模式,周模式. 顯示農(nóng)歷,標(biāo)記時(shí)間.定制時(shí)間范圍.選擇事件等多種需求.
一翘盖、安裝
1. 使用CocoaPods安裝 (安裝流程不敘述了,直接搜索FSCalendar 最新版安裝)
2. 手動(dòng)安裝(github地址: https://github.com/WenchaoD/FSCalendar) 下載之后直接把FSCalendar文件夾拖進(jìn)你的項(xiàng)目中,確保Copy items if needed被選中即可.
二凹蜂、使用
1 引入頭文件.? 2 添加代理 . 3 聲明FSCalendar?
4 添加FSCalendar
5 關(guān)于顏色設(shè)置和文字設(shè)置參考下面圖片
三馍驯、 顯示農(nóng)歷
1 創(chuàng)建
? ?獲取農(nóng)歷、事件玛痊、節(jié)假日汰瘫、節(jié)氣等信息,通過NSCalendar和EventKit完成
//添加頭文件
#import <EventKit/EventKit.h>
//創(chuàng)建Calendar
@property (strong, nonatomic) NSCalendar *chineseCalendar;
//特殊節(jié)日數(shù)組
@property (strong, nonatomic) NSArray<EKEvent *> ?*events;
//農(nóng)歷數(shù)組
@property (strong, nonatomic) NSArray<NSString *> ?*lunarChars;
2 添加數(shù)據(jù)
//初始化
self.chineseCalendar = [NSCalendar calendarWithIdentifier:NSCalendarIdentifierChinese];? ?
//添加農(nóng)歷數(shù)組
self.lunarChars = @[@"初一",@"初二",@"初三",@"初四",@"初五",@"初六",@"初七",@"初八",@"初九",@"初十",@"十一",@"十二",@"十三",@"十四",@"十五",@"十六",@"十七",@"十八",@"十九",@"二十",@"二一",@"二二",@"二三",@"二四",@"二五",@"二六",@"二七",@"二八",@"二九",@"三十"];? ?
//添加特殊節(jié)日數(shù)組
? ? __weak typeof(self) weakSelf = self;? ? ? ?
?EKEventStore *store = [[EKEventStore alloc] init];? ? ? ? ? ? ?
?? [store requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {? ? ? ? if(granted) {? ? ? ? ? ? NSDate *startDate = [NSDate dateWithTimeIntervalSinceNow:-3600*24*90];; // 開始日期? ? ? ? ? ? NSDate *endDate =[NSDate dateWithTimeIntervalSinceNow:3600*24*90]; // 截止日期? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? NSPredicate *fetchCalendarEvents = [store predicateForEventsWithStartDate:startDate endDate:endDate calendars:nil];? ? ? ? ? ? NSArray*eventList = [store eventsMatchingPredicate:fetchCalendarEvents];? ? ? ? ? ? NSArray*events = [eventList filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(EKEvent * _Nullable event, NSDictionary* _Nullable bindings) {
return event.calendar.subscribed;
}]];
weakSelf.events = events;
}
}];
3 實(shí)現(xiàn)并顯示到控件
- (NSString *)calendar:(FSCalendar *)calendar subtitleForDate:(NSDate *)date{? ?
?EKEvent *event = [self eventsForDate:date].firstObject;? ? ? ?
?if (event) {? ? ? ? ? ? return event.title;? ? }? ? ??
? NSInteger day = [_chineseCalendar component:NSCalendarUnitDay fromDate:date];?
?? return _lunarChars[day-1];
}//代理添加 農(nóng)歷顯示
- (NSArray*)eventsForDate:(NSDate *)date{ ??
? ? NSArray*filteredEvents = [self.events filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(EKEvent * _Nullable evaluatedObject, NSDictionary* _Nullable bindings) {
return [evaluatedObject.occurrenceDate isEqualToDate:date];
}]];
return filteredEvents;
}//返回特殊節(jié)日的方法
3 標(biāo)記特殊位置方法(圓點(diǎn))
- (NSInteger)calendar:(FSCalendar *)calendar numberOfEventsForDate:(NSDate *)date{
//要標(biāo)記的日期顯示圓點(diǎn)3個(gè)其他不顯示 ?
if ([[self.dateFormatter stringFromDate:date] isEqualToString:[self.dateFormatter stringFromDate:self.DifferenceDate]]) { ? ? ??
return 3;
}else? ? {? ? ? ? //特殊日期標(biāo)記
//? ? ? ? _Calendar.appearance.eventDefaultColor=[UIColor blueColor];//? ? ? ? //? ? ? ? ? ? NSArray*events = [self eventsForDate:date];
//? ? ? ? ? ? return events.count;
return 0;
}
}
四擂煞、 其他代理方法
1 時(shí)間選擇事件
-(void)calendar:(FSCalendar *)calendar didSelectDate:(NSDate *)date atMonthPosition:(FSCalendarMonthPosition)monthPosition
{
NSString *str =[NSString stringWithFormat:@"確認(rèn)(%@)",[self.dateFormatter stringFromDate:date]];
[self.DateVerify setTitle:str forState:UIControlStateNormal];
}
2 設(shè)置今天文字為"今",其他不變.
- (NSString *)calendar:(FSCalendar *)calendar titleForDate:(NSDate *)date{
if ([self.chineseCalendar isDateInToday:date])?
{return @"今";}
return nil;
}
3 設(shè)置可選擇的時(shí)間范圍
//下面的時(shí)間是2015.1.1~今天的范圍
//最小時(shí)間2015.1.1
- (NSDate *)minimumDateForCalendar:(FSCalendar *)calendar
{
NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
return [fmt dateFromString:@"2015-01-01"];
}
//最大時(shí)間今天
- (NSDate *)maximumDateForCalendar:(FSCalendar *)calendar
{
return [NSDate date];
}
五混弥、 特殊需求定制
1,回到今天 ,上一個(gè)月和下一個(gè)月.滾動(dòng)動(dòng)畫.
一 FSCalendar 給出了方法去到指定時(shí)間的方法
[self.Calendar setCurrentPage:[NSDate date] animated:YES];//回到今天(你需要去到的時(shí)間date和是否顯示動(dòng)畫)
二 ?上一個(gè)月和下一個(gè)月
//上一個(gè)月
NSDate *nextMonth = [self.chineseCalendar dateByAddingUnit:NSCalendarUnitMonth value:-1 toDate:self.Calendar.currentPage options:0];
[self.Calendar setCurrentPage:nextMonth animated:YES];
//下一個(gè)月
NSDate *nextMonth = [self.chineseCalendar dateByAddingUnit:NSCalendarUnitMonth value:1 toDate:self.Calendar.currentPage options:0];
[self.Calendar setCurrentPage:nextMonth animated:YES];
2 ?周模式和月模式的切換和應(yīng)用
一 ,FSCalendar添加手勢(shì) 左右輕掃滑動(dòng)是翻頁(yè) 上下輕掃就是展開與閉合
UISwipeGestureRecognizer *SwipeDown =[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(SwipeDownAction:)];
SwipeDown.direction =UISwipeGestureRecognizerDirectionDown;
[self.Calendar addGestureRecognizer:SwipeDown];
UISwipeGestureRecognizer *SwipeUp =[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(SwipeUpAction:)];
SwipeUp.direction =UISwipeGestureRecognizerDirectionUp;
[self.Calendar addGestureRecognizer:SwipeUp];
二 手勢(shì)事件(FSCalendar同樣給出方法)
-(void)SwipeDownAction:(UISwipeGestureRecognizer *)Action{
[self.Calendar setScope:FSCalendarScopeMonth animated:YES];
}
-(void)SwipeUpAction:(UISwipeGestureRecognizer *)Action{
[self.Calendar setScope:FSCalendarScopeWeek animated:YES];
}
三 難點(diǎn)!(TableView怎樣聯(lián)動(dòng)FSCalendar周和月的高度)
FSCalendar 代理同樣給出了動(dòng)畫事件獲取高度
- (void)calendar:(FSCalendar *)calendar boundingRectWillChange:(CGRect)bounds animated:(BOOL)animated
{
//bounds可以獲取高度
//動(dòng)畫結(jié)束更新TableView大小位置(請(qǐng)查看SDAutoLayout使用)
[self.tableView updateLayout];
/*
1 第一步給FSCalendar子控件添加tag值
for (int i = 0; i<_Calendar.subviews.count; i++) {
UIView *son =_Calendar.subviews[i];
son.tag=i+100;
}
2 拿出FSCalendar周視圖view
self.tableView.sd_layout
.leftSpaceToView(self.view,0)//靠左距離為0
.topSpaceToView([self.Calendar viewWithTag:100+0],0)//靠上周視圖的距離0
.rightSpaceToView(self.view,0)//右
.bottomSpaceToView(self.view,0);//下
每次動(dòng)畫結(jié)束更新這個(gè)即可
*/
}
3 . 時(shí)間段選擇
一 設(shè)置和添加?
//設(shè)置多選
_calendar.allowsMultipleSelection = YES;
//添加 開始時(shí)間,結(jié)束時(shí)間. 選擇狀態(tài)
@property(nonatomic,copy)NSDate *BeginDate;
@property(nonatomic,copy)NSDate *EndDate;
@property(nonatomic,assign)BOOL SelectAction;
二? 設(shè)置 定制效果(默認(rèn)一個(gè)范圍,每次選擇都重新開始.)?
//(默認(rèn)選擇前七天)
self.BeginDate= [[NSDate date] dateByAddingTimeInterval:-6*60*60*24];
self.EndDate=[NSDate date];
for (int i = 0; i<7; i++) {
[_calendar selectDate:[[NSDate date] dateByAddingTimeInterval:-i*60*60*24]];
}
//設(shè)置開始和結(jié)束標(biāo)題
- (NSString *)calendar:(FSCalendar *)calendar titleForDate:(NSDate *)date{
if (self.SelectAction) {
if ([[self.dateFormatter stringFromDate:self.BeginDate] isEqualToString:[self.dateFormatter stringFromDate:date]]) {
return @"開始";
}else{ return nil; }
}else{
if ([[self.dateFormatter stringFromDate:self.BeginDate] isEqualToString:[self.dateFormatter stringFromDate:date]]) {
return @"開始";
}else if ([[self.dateFormatter stringFromDate:self.EndDate] isEqualToString:[self.dateFormatter stringFromDate:date]]){
return @"結(jié)束";
}else{
return nil;
}}}
//設(shè)置
//選中某一天進(jìn)行相關(guān)操作
- (void)calendar:(FSCalendar *)calendar didSelectDate:(NSDate *)date {
if (!self.SelectAction) {
NSArray *selectArrray =[calendar selectedDates];
for (NSDate *select in selectArrray) {
[calendar deselectDate:select];
}
[_calendar selectDate:date];
self.SelectAction=YES;
self.BeginDate=date;
self.EndDate=nil;
[_calendar reloadData];
}else
{
NSInteger number =[self numberOfDaysWithFromDate:self.BeginDate? ? toDate:date];
NSLog(@"%ld",number);
if (number<0) {
self.SelectAction=YES;
UIAlertController? ? *AlertController =[UIAlertController alertControllerWithTitle:nil message:@"小于開始日期,請(qǐng)重新選擇" preferredStyle:UIAlertControllerStyleAlert];
AppDelegate *delegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
[delegate.window.rootViewController presentViewController:self.Alert animated:YES completion:nil];
dispatch_time_t time = dispatch_time(DISPATCH_TIME_NOW, 1* NSEC_PER_SEC);
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_after(time, queue, ^{
dispatch_async(dispatch_get_main_queue(), ^{
[AlertController dismissViewControllerAnimated:YES completion:^{}];
[calendar deselectDate:date];
});
});
}else
{
self.SelectAction=NO;
self.EndDate=date;
for (int i = 0; i<number; i++) {
[_calendar selectDate:[date dateByAddingTimeInterval:-i*60*60*24]];
}
[_calendar reloadData];
}}}
//點(diǎn)擊選擇中范圍
-(void)calendar:(FSCalendar *)calendar didDeselectDate:(NSDate *)date atMonthPosition:(FSCalendarMonthPosition)monthPosition
{
NSArray *selectArrray =[calendar selectedDates];
for (NSDate *select in selectArrray) {
[calendar deselectDate:select];
}
[_calendar selectDate:date];
self.SelectAction=YES;
self.BeginDate=date;
self.EndDate=nil;
[_calendar reloadData];
}
#pragma -mark 計(jì)算日期差的方法
-(NSInteger)numberOfDaysWithFromDate:(NSDate *)fromDate toDate:(NSDate *)toDate{
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
NSDateComponents? ? * comp = [calendar components:NSCalendarUnitDay
fromDate:fromDate
toDate:toDate
options:NSCalendarWrapComponents];
return comp.day;
}