FSCalendar 使用!定制你想要的一款日歷控件

? ? ?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é)日的方法


農(nóng)歷顯示


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;

}

}

//取消今天的顏色設(shè)置為白色或者原色就好了

四擂煞、 其他代理方法

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è)置和添加?

//設(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;

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子蝗拿,更是在濱河造成了極大的恐慌晾捏,老刑警劉巖,帶你破解...
    沈念sama閱讀 221,198評(píng)論 6 514
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件哀托,死亡現(xiàn)場(chǎng)離奇詭異惦辛,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)仓手,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,334評(píng)論 3 398
  • 文/潘曉璐 我一進(jìn)店門胖齐,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人嗽冒,你說我怎么就攤上這事呀伙。” “怎么了添坊?”我有些...
    開封第一講書人閱讀 167,643評(píng)論 0 360
  • 文/不壞的土叔 我叫張陵剿另,是天一觀的道長(zhǎng)。 經(jīng)常有香客問我帅腌,道長(zhǎng)驰弄,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 59,495評(píng)論 1 296
  • 正文 為了忘掉前任速客,我火速辦了婚禮戚篙,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘溺职。我一直安慰自己岔擂,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,502評(píng)論 6 397
  • 文/花漫 我一把揭開白布浪耘。 她就那樣靜靜地躺著乱灵,像睡著了一般。 火紅的嫁衣襯著肌膚如雪七冲。 梳的紋絲不亂的頭發(fā)上痛倚,一...
    開封第一講書人閱讀 52,156評(píng)論 1 308
  • 那天,我揣著相機(jī)與錄音澜躺,去河邊找鬼蝉稳。 笑死,一個(gè)胖子當(dāng)著我的面吹牛掘鄙,可吹牛的內(nèi)容都是我干的耘戚。 我是一名探鬼主播,決...
    沈念sama閱讀 40,743評(píng)論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼操漠,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼收津!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,659評(píng)論 0 276
  • 序言:老撾萬榮一對(duì)情侶失蹤撞秋,失蹤者是張志新(化名)和其女友劉穎长捧,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體部服,經(jīng)...
    沈念sama閱讀 46,200評(píng)論 1 319
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡唆姐,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,282評(píng)論 3 340
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了廓八。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片奉芦。...
    茶點(diǎn)故事閱讀 40,424評(píng)論 1 352
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖剧蹂,靈堂內(nèi)的尸體忽然破棺而出声功,到底是詐尸還是另有隱情,我是刑警寧澤宠叼,帶...
    沈念sama閱讀 36,107評(píng)論 5 349
  • 正文 年R本政府宣布先巴,位于F島的核電站,受9級(jí)特大地震影響冒冬,放射性物質(zhì)發(fā)生泄漏伸蚯。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,789評(píng)論 3 333
  • 文/蒙蒙 一简烤、第九天 我趴在偏房一處隱蔽的房頂上張望剂邮。 院中可真熱鬧,春花似錦横侦、人聲如沸挥萌。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,264評(píng)論 0 23
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽引瀑。三九已至,卻和暖如春榨馁,著一層夾襖步出監(jiān)牢的瞬間憨栽,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,390評(píng)論 1 271
  • 我被黑心中介騙來泰國(guó)打工翼虫, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留屑柔,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 48,798評(píng)論 3 376
  • 正文 我出身青樓蛙讥,卻偏偏與公主長(zhǎng)得像锯蛀,于是被迫代替她去往敵國(guó)和親灭衷。 傳聞我的和親對(duì)象是個(gè)殘疾皇子次慢,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,435評(píng)論 2 359

推薦閱讀更多精彩內(nèi)容