iOS高可控性日历基础组件-SKCalendarView的使用和实现

移动开发 iOS
SKCalendarView是一个高可控性的日历基础组件,为了提高应用的自由度,默认只提供了日历部分的视图封装,但不涵盖切换月份按钮、年月分显示等非关键性控件,但请不要担心,SKCalendarView为你提供了多样性的API,你可以很轻松的拿到这些信息去展示在你自己的自定义控件中,以及对当前UI的修改:如:替换日历主题图片、节假日或特殊日期的日历背景、各种现实颜色等等。

简述

SKCalendarView是一个高可控性的日历基础组件,为了提高应用的自由度,默认只提供了日历部分的视图封装,但不涵盖切换月份按钮、年月分显示等非关键性控件,但请不要担心,SKCalendarView为你提供了多样性的API,你可以很轻松的拿到这些信息去展示在你自己的自定义控件中,以及对当前UI的修改:如:替换日历主题图片、节假日或特殊日期的日历背景、各种现实颜色等等。不仅如此,SKCalendarView还为你封装了公历、农历、节假日以及中国24节气的核心算法,即使你觉得默认的视图并不合胃口,也可以直接快速的利用这套算法创造出一个全新的日历控件。***,SKCalendarView还提供了一些简单的切换动画,如果你不喜欢它,可以忽略掉,用自己的,这里完全不会受到任何限制。如果觉得还不错,点个star吧~ 

 

 

[[190339]] 

效果图 

 

 

 

***更新:

  • 发布了0.0.3版本
  • 对日历算法部分进行优化,完全解决线程卡顿问题
  • 修复了指定日期查寻在显示上的若干问题

一.如何使用

1.如何开始

1).从GitHub上Clone-->SKCalendarView, 然后查看Demo (由于使用cocoaPods管理,请打开xcworkspace工程进行查看)

2).在项目中使用SKCalendarView,直接将目录下的SKCalendarView文件夹拷贝到工程中,或在podfile文件中添加pod 'SKCalendarView'

3).SKCalendarView的默认视图基于Masonry布局,如果需要使用, 请确保你的工程里已存在Masonry,下载地址

4).如果遇到其它问题,欢迎提交issues,我会及时回复

2.使用方法

  • 头文件导入
  1. #import "SKConstant.h" 
  • 继承SKCalendarView
  1. @property (nonatomic, strong) SKCalendarView * calendarView; 

日历设置

  1. _calendarView.calendarTodayTitleColor = [UIColor redColor];// 今天标题字体颜色 
  2. _calendarView.calendarTodayTitle = @"今日";// 今天下标题 
  3. _calendarView.dateColor = [UIColor orangeColor];// 今天日期数字背景颜色 
  4. _calendarView.calendarTodayColor = [UIColor whiteColor];// 今天日期字体颜色 
  5. _calendarView.dayoffInWeekColor = [UIColor redColor]; 
  6. _calendarView.springColor = [UIColor colorWithRed:48 / 255.0 green:200 / 255.0 blue:104 / 255.0 alpha:1];// 春季节气颜色 
  7. _calendarView.summerColor = [UIColor colorWithRed:18 / 255.0 green:96 / 255.0 blue:0 alpha:8];// 夏季节气颜色 
  8. _calendarView.autumnColor = [UIColor colorWithRed:232 / 255.0 green:195 / 255.0 blue:0 / 255.0 alpha:1];// 秋季节气颜色 
  9. _calendarView.winterColor = [UIColor colorWithRed:77 / 255.0 green:161 / 255.0 blue:255 / 255.0 alpha:1];// 冬季节气颜色 
  10. _calendarView.holidayColor = [UIColor redColor];//节日字体颜色 
  11. self.lastMonth = _calendarView.lastMonth;// 获取上个月的月份 
  12. self.nextMonth = _calendarView.nextMonth;// 获取下个月的月份  

翻页动画

  1. [SKCalendarAnimationManage animationWithView:self.calendarView andEffect:SK_ANIMATION_REVEAL isNext:YES]; 

获取农历年

  1. self.chineseYearLabel.text = [NSString stringWithFormat:@"%@年", self.calendarView.chineseYear];// 农历年 

获取农历月日

  1. self.chineseMonthAndDayLabel.text = [NSString stringWithFormat:@"%@%@", self.calendarView.chineseMonth, getNoneNil(self.calendarView.chineseCalendarDay[row])]; 

获取公历年/月

  1. self.yearLabel.text = [NSString stringWithFormat:@"%@年%@月", @(self.calendarView.year), @(self.calendarView.month)];// 公历年 

获取节日/节气

  1. self.holidayLabel.text = [self.calendarView getHolidayAndSolarTermsWithChineseDay:getNoneNil(self.calendarView.chineseCalendarDay[row])]; 

查询指定日期

  1. [self.calendarView checkCalendarWithAppointDate:[NSDate date]]; 

日历UI配置

  1. @property (nonatomic, strong) UIColor * weekBackgroundColor;// 周的背景颜色 
  2. @property (nonatomic, strong) UIColor * normalInWeekColor;// 周(除双休日外)字体颜色 
  3. @property (nonatomic, strong) UIColor * dayoffInWeekColor;// 双休日字体颜色 
  4. @property (nonatomic, strong) UIColor * calendarTodayColor;// 本日日期字体颜色 
  5. @property (nonatomic, strong) UIColor * dateColor;// 日期小背景颜色 
  6. @property (nonatomic, strong) UIImage * dateIcon;// 日期图片 
  7. @property (nonatomic, strong) UIColor * holidayBackgroundColor;// 节日背景颜色 
  8. @property (nonatomic, strong) UIColor * solarTeromBackgroundColor;// 节气背景颜色 
  9. @property (nonatomic, strong) UIColor * dateBackgroundColor;// 日期背景颜色(非节日&节气) 
  10. @property (nonatomic, strong) UIImage * dateBackgroundIcon;// 日期背景图片 
  11. @property (nonatomic, strong) NSString * calendarTodayTitle;// 本日日期标题 
  12. @property (nonatomic, strong) UIColor * calendarTodayTitleColor;// 本日日期标题字体颜色 
  13. @property (nonatomic, strong) UIColor * calendarTitleColor;// 日期标题字体颜色 
  14. @property (nonatomic, strong) UIColor * holidayColor;// 节日标题字体颜色 
  15. @property (nonatomic, strong) UIColor * springColor;// 春季节气颜色 
  16. @property (nonatomic, strong) UIColor * summerColor;// 夏季节气颜色 
  17. @property (nonatomic, strong) UIColor * autumnColor;// 秋季节气颜色 
  18. @property (nonatomic, strong) UIColor * winterColor;// 冬季节气颜色 
  19. @property (nonatomic, assign) BOOL enableClickEffect;// 开启点击效果 
  20. @property (nonatomic, assign) BOOL enableDateRoundCorner;// 开启日期圆角  

获取点击到的日期

注意:这里需要先遵循代理协议

  1. - (void)selectDateWithRow:(NSUInteger)row 

二.如何实现

1.设计思路

  • 总体上SKCalendarView仍然才去模块化思路,主要分为三个部分View(视图)、Animation(动画)以及Algorithm(算法)
  • View主要负责处理外部对UI的配置信息、日历核心部分的展示、UI的刷新、效果的处理和界面控件的创建和布局约束等
  • Animation主要负责日历翻页时的动画效果及点击日期的动画效果的处理
  • Algorithm是整个SKCalendarView最核心的部分,负责了公历、农历、节假日以及中国24节气的核心算法,以及对日期查询的处理反馈

2.功能实现

2.1布局

思路

我们先要搞清楚日历是什么。所谓日历,就是一年当中12个月份的日期展示,每个月当中的日期数量由28~31天不等,这里指的是公历, 而农历当中每个月最多30天,虽然在计算方法上是有很大差别,但好在当代日历都是以公历为展示基准,所以只需要考虑公历的每月天数。

因为要考虑到展示上的美观性,一般都是采用正方形来展示,由于一周是固定的7天,所以我们日历的横向子控件数量也必须为7。但是这样问题就来了,由于需要考虑到与日期上方的周时间相对应,并且除了2月没有哪个月是的天数的7的倍数,也就做不到整除而导致无法形成正方形布局,所以我们不能直接用和月份天数相等的子控件数量来展示我们的日历,经过思考,我决定采取填充数据的方式来达到正方形展示的目的:

  • 首先规划整体子控件数量,由于横向固定是7,那么纵向就由最多的一个月31天算,31 / 7 ≈ 4.4, 既然超过了4行,那么我们就放5行吧: 5 x 7 = 35,子控件放置35个如何?但经过尝试后,发现这并不可取:因为我们这里理想状态下的31天是以这个月的***天恰好是周日 (周日为公历一周的开始) 为前提条件的,那么显然在现实生活里并不可能每个月都恰好***天都是周日,所以,我们就需要考虑到需要显示的这个月的***天是周几这个问题,众所周知,一周有7天的时间,那么每个月的***天就有7种可能。做最多的打算,假设这个月总共有31天,而***天恰好是周六,那么在这个月的1日这一天之前就有6天是没有日期的,结合我们之前计算的数量加上周六前的6天: 35 + 6 = 41,子控件放41个又如何呢?当然是不行了,因为需要正方形的日历,所以至少要成为7的倍数,最接近这个倍数的值就是我们要的答案:42.

实现

  • 在基础控件的布局上,我们采取最简便的方式:周和日期我们分别使用了weekCollectionView、calendarCollectionView这两个UICollectionView来完成
  • 而月份的背景数字monthBackgroundLabel作为最上面一层采用的是UILabel,在设置了其size和weight后,效果就如同背景图一样
  1. // 周 
  2.   UICollectionViewFlowLayout * layout = [[UICollectionViewFlowLayout alloc] init]; 
  3.   layout.scrollDirection = UICollectionViewScrollDirectionVertical; 
  4.   self.weekCollectionView = [[UICollectionView alloc] initWithFrame:self.frame collectionViewLayout:layout]; 
  5.   [self addSubview:self.weekCollectionView]; 
  6.   self.weekCollectionView.backgroundColor = [UIColor whiteColor]; 
  7.   self.weekCollectionView.delegate = self; 
  8.   self.weekCollectionView.dataSource = self; 
  9.   [self.weekCollectionView registerClass:[SKWeekCollectionViewCell class] forCellWithReuseIdentifier:@"Week"]; 
  10.   [self.weekCollectionView mas_makeConstraints:^(MASConstraintMaker *make) { 
  11.       make.top.equalTo(self); 
  12.       make.left.equalTo(self); 
  13.       make.right.equalTo(self); 
  14.       make.height.mas_offset(self.frame.size.height / 7.5); 
  15.       make.height.mas_greaterThanOrEqualTo(40).priorityHigh(); 
  16.   }]; 
  17.   // 日期 
  18.   UICollectionViewFlowLayout * dateLayout = [[UICollectionViewFlowLayout alloc] init]; 
  19.   dateLayout.scrollDirection = UICollectionViewScrollDirectionVertical; 
  20.   self.calendarCollectionView = [[UICollectionView alloc] initWithFrame:self.frame collectionViewLayout:dateLayout]; 
  21.   [self addSubview:self.calendarCollectionView]; 
  22.   self.calendarCollectionView.backgroundColor = [UIColor whiteColor]; 
  23.   self.calendarCollectionView.delegate = self; 
  24.   self.calendarCollectionView.dataSource = self; 
  25.   [self.calendarCollectionView registerClass:[SKCalendarCollectionViewCell class] forCellWithReuseIdentifier:@"Calendar"]; 
  26.   [self.calendarCollectionView mas_makeConstraints:^(MASConstraintMaker *make) { 
  27.       make.top.equalTo(self.weekCollectionView.mas_bottom); 
  28.       make.left.equalTo(self); 
  29.       make.right.equalTo(self); 
  30.       make.bottom.equalTo(self); 
  31.   }]; 
  32.   // 背景月份 
  33.   self.monthBackgroundLabel = [UILabel new]; 
  34.   [self addSubview:self.monthBackgroundLabel]; 
  35.   self.monthBackgroundLabel.textColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:200 / 2550.f]; 
  36.   self.monthBackgroundLabel.font = [UIFont systemFontOfSize:150.0f weight:120.f]; 
  37.   self.monthBackgroundLabel.textAlignment = NSTextAlignmentCenter; 
  38.   [self.monthBackgroundLabel mas_makeConstraints:^(MASConstraintMaker *make) { 
  39.       make.edges.equalTo(self).with.insets(UIEdgeInsetsMake(0, 0, 0, 0)); 
  40.   }];  

对日历高度的控制

  • 由于不同的月份的***天所处的周时间不同,导致日历的有效日期 (有日期显示的) 行数不固定,如:当本月***天为周日时,最多只占35个子控件位数,而我们一开始设置的子控件数量值是42,这样一来就会空出一行的空白出来,这是很不美观的。所以日历的高度对于我们来说就是一个把控的值,如何来保证可以根据每个月的天数来控制日历的高度呢,在SKCalendarView中采取了以下的办法:
  1. if (self.calendarManage.isIncreaseHeight == YES) {// 根据isIncreaseHeight来判断是否需要更改高度 
  2.           [self.calendarCollectionView mas_updateConstraints:^(MASConstraintMaker *make) { 
  3.               make.height.mas_offset(6 * (self.frame.size.height / 7.5)); 
  4.           }]; 
  5.           return 42; 
  6.   
  7.       } else { 
  8.           if (self.calendarCollectionView.frame.size.height > 218) { 
  9.               [self.calendarCollectionView mas_updateConstraints:^(MASConstraintMaker *make) { 
  10.                   make.height.mas_offset(5 * (self.frame.size.height / 7.5)); 
  11.               }]; 
  12.           } 
  13.           return 35; 
  14.       }  

日期点击效果的处理

  • 在SKCalendarCollectionViewCell的内部,我们将enableClickEffect(是否开启点击效果)为YES的状态设为开启效果,并调用动画管理类SKCalendarAnimationManage的方法
  1. [SKCalendarAnimationManage clickEffectAnimationForView:self.baseView]; 

2.2 日历算法

这一部分算法是整个SKCalendarView最核心的部分

  • SKCalendarManage以单例的模式封装了SKCalendarView全部的核心算法
  • 主要难点在于对个别不定期节日,如复活节的日期的计算等,以及24节气和农历的计算,推荐阅读《算法:计算中国农历》
  • 查看所选日期所处的月份:
  1. #pragma mark - 查看所选日期所处的月份 
  2. - (void)checkThisMonthRecordFromToday:(NSDate *)today 
  3.     if (isEmpty(today)) {// 如果没有日期,默认今天 
  4.         today = [NSDate date]; 
  5.     } 
  6.     [self calculationThisMonthDays:today];// 计算本月天数 
  7.     [self calculationThisMonthFirstDayInWeek:today];// 计算本月***天是周几 
  8.  
  • 计算本月天数
  1. #pragma mark - 计算本月天数 
  2. - (void)calculationThisMonthDays:(NSDate *)days 
  3.     NSCalendar * calendar = [NSCalendar currentCalendar]; 
  4.     if (isEmpty(days)) { 
  5.         days = [NSDate date]; 
  6.     } 
  7.     NSRange range = [calendar rangeOfUnit:NSCalendarUnitDay inUnit:NSCalendarUnitMonth forDate:days]; 
  8.     self.days = range.length;// 保存天数 
  9.  
  • 计算本月***天是周几
  1. #pragma mark - 计算本月***天是周几 
  2. - (void)calculationThisMonthFirstDayInWeek:(NSDate *)date
  3.     if (isEmpty(date)) { 
  4.         date = [NSDate date]; 
  5.     } 
  6.     NSCalendar * calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian]; 
  7.     NSDateComponents * comps = [[NSDateComponents alloc] init]; 
  8.     NSDateComponents * theComps = [[NSDateComponents alloc] init]; 
  9.     NSInteger unitFlags = NSCalendarUnitDay | NSCalendarUnitWeekday | NSCalendarUnitMonth | NSCalendarUnitYear; 
  10.     comps = [calendar components:unitFlags fromDate:date]; 
  11.     theComps = [calendar components:unitFlags fromDate:[NSDate date]]; 
  12.     self.theMonth = [theComps month];// 本月的月份 
  13.     NSUInteger day = [comps day];// 是本月第几天 
  14.     self.todayInMonth = day
  15.     if (day > 1) {// 如果不是本月***天 
  16.         // 将日期推算到本月***天 
  17.         NSInteger hours = (day - 1) * -24; 
  18.         date = [NSDate dateWithTimeInterval:hours * 60 * 60 sinceDate:date]; 
  19.     } 
  20.     comps = [calendar components:unitFlags fromDate:date]; 
  21.     self.dayInWeek = [comps weekday];// 是周几 
  22.     self.year = [comps year];// 公历年 
  23.     self.month = [comps month];// 公里月 
  24.     [self creatcalendarArrayWithDate:date];// 创建日历数组 
  25.  
  • 创建日历数组(公历、农历)

这里的算法还有优化的必要,如果有朋友可以指点一二,不胜感激 

  •  
  1. #pragma mark - 创建日历数组 
  2. - (void)creatcalendarArrayWithDate:(NSDate *)date 
  3.     self.calendarDate = [NSMutableArray new]; 
  4.     self.chineseCalendarDate = [NSMutableArray new]; 
  5.     self.chineseCalendarDay = [NSMutableArray new]; 
  6.     for (NSInteger j = 0; j < 42; j ++) {// 创建空占位数组 
  7.         [self.calendarDate addObject:@""]; 
  8.         [self.chineseCalendarDate addObject:@""]; 
  9.         [self.chineseCalendarDay addObject:@""]; 
  10.     } 
  11.     // 向前推算日期到本月***天 
  12.     NSDate * firstDay = date
  13.     self.todayInMonth = self.todayInMonth + self.dayInWeek - 2;// 计算在本月日历上所处的位置 
  14.     switch (self.dayInWeek) {// 根据本月***天是周几,来确定之后的日期替换空占位 
  15.         case 1:// 周日 
  16.             for (NSInteger i = 1; i <= self.days; i ++) { 
  17.                 [self.calendarDate replaceObjectAtIndex:i - 1 withObject:@(i)];// 替换公历日期 
  18.                 for (NSInteger j = 1; j <= self.days; j ++) {// 公历日期 
  19.                     // 向后推算至本月末 
  20.                     NSInteger hours = (j - 1) * 24; 
  21.                     NSDate * date = [NSDate dateWithTimeInterval:hours * 60 * 60 sinceDate:firstDay]; 
  22.                     NSString * chineseDay = [self calculationChinaCalendarWithDate:date dispalyHoliday:YES]; 
  23.                     [self.chineseCalendarDate replaceObjectAtIndex:j - 1 withObject:chineseDay];// 替换农历日期 
  24.                     NSString * noHoliday = [self calculationChinaCalendarWithDate:date dispalyHoliday:NO]; 
  25.                     [self.chineseCalendarDay replaceObjectAtIndex:j - 1 withObject:noHoliday];// 替换纯农历日期(无节假日) 
  26.                 } 
  27.             } 
  28.             self.isIncreaseHeight = NO
  29.             break; 
  30.         case 2:// 周一 
  31.             for (NSInteger i = 1; i = 2) { 
  32.                     [self.calendarDate replaceObjectAtIndex:i - 1 withObject:@(i - 1)]; 
  33.                     for (NSInteger j = 1; j = 2) { 
  34.                             // 向后推算至本月末 
  35.                             NSInteger hours = (j - 2) * 24; 
  36.                             NSDate * date = [NSDate dateWithTimeInterval:hours * 60 * 60 sinceDate:firstDay]; 
  37.                             NSString * chineseDay = [self calculationChinaCalendarWithDate:date dispalyHoliday:YES]; 
  38.                             [self.chineseCalendarDate replaceObjectAtIndex:j - 1 withObject:chineseDay];// 替换农历日期 
  39.                             NSString * noHoliday = [self calculationChinaCalendarWithDate:date dispalyHoliday:NO]; 
  40.                             [self.chineseCalendarDay replaceObjectAtIndex:j - 1 withObject:noHoliday]; 
  41.                         } 
  42.                     } 
  43.                 } 
  44.             } 
  45.             self.isIncreaseHeight = NO
  46.             break; 
  47.         case 3:// 周二 
  48.             for (NSInteger i = 1; i = 3) { 
  49.                     [self.calendarDate replaceObjectAtIndex:i - 1 withObject:@(i - 2)]; 
  50.                     for (NSInteger j = 1; j = 3) { 
  51.                             // 向后推算至本月末 
  52.                             NSInteger hours = (j - 3) * 24; 
  53.                             NSDate * date = [NSDate dateWithTimeInterval:hours * 60 * 60 sinceDate:firstDay]; 
  54.                             NSString * chineseDay = [self calculationChinaCalendarWithDate:date dispalyHoliday:YES]; 
  55.                             [self.chineseCalendarDate replaceObjectAtIndex:j - 1 withObject:chineseDay];// 替换农历日期 
  56.                             NSString * noHoliday = [self calculationChinaCalendarWithDate:date dispalyHoliday:NO]; 
  57.                             [self.chineseCalendarDay replaceObjectAtIndex:j - 1 withObject:noHoliday]; 
  58.                         } 
  59.                     } 
  60.                 } 
  61.             } 
  62.             self.isIncreaseHeight = NO
  63.             break; 
  64.         case 4:// 周三 
  65.             for (NSInteger i = 1; i = 4) { 
  66.                     [self.calendarDate replaceObjectAtIndex:i - 1 withObject:@(i - 3)]; 
  67.                     for (NSInteger j = 1; j = 4) { 
  68.                             // 向后推算至本月末 
  69.                             NSInteger hours = (j - 4) * 24; 
  70.                             NSDate * date = [NSDate dateWithTimeInterval:hours * 60 * 60 sinceDate:firstDay]; 
  71.                             NSString * chineseDay = [self calculationChinaCalendarWithDate:date dispalyHoliday:YES]; 
  72.                             [self.chineseCalendarDate replaceObjectAtIndex:j - 1 withObject:chineseDay];// 替换农历日期 
  73.                             NSString * noHoliday = [self calculationChinaCalendarWithDate:date dispalyHoliday:NO]; 
  74.                             [self.chineseCalendarDay replaceObjectAtIndex:j - 1 withObject:noHoliday]; 
  75.                         } 
  76.                     } 
  77.                 } 
  78.             } 
  79.             self.isIncreaseHeight = NO
  80.             break; 
  81.         case 5:// 周四 
  82.             for (NSInteger i = 1; i = 5) { 
  83.                     [self.calendarDate replaceObjectAtIndex:i - 1 withObject:@(i - 4)]; 
  84.                     for (NSInteger j = 1; j = 5) { 
  85.                             // 向后推算至本月末 
  86.                             NSInteger hours = (j - 5) * 24; 
  87.                             NSDate * date = [NSDate dateWithTimeInterval:hours * 60 * 60 sinceDate:firstDay]; 
  88.                             NSString * chineseDay = [self calculationChinaCalendarWithDate:date dispalyHoliday:YES]; 
  89.                             [self.chineseCalendarDate replaceObjectAtIndex:j - 1 withObject:chineseDay];// 替换农历日期 
  90.                             NSString * noHoliday = [self calculationChinaCalendarWithDate:date dispalyHoliday:NO]; 
  91.                             [self.chineseCalendarDay replaceObjectAtIndex:j - 1 withObject:noHoliday]; 
  92.                         } 
  93.                     } 
  94.                 } 
  95.             } 
  96.             self.isIncreaseHeight = NO
  97.             break; 
  98.         case 6:// 周五 
  99.             for (NSInteger i = 1; i = 6) { 
  100.                     [self.calendarDate replaceObjectAtIndex:i - 1 withObject:@(i - 5)]; 
  101.                     for (NSInteger j = 1; j = 6) { 
  102.                             // 向后推算至本月末 
  103.                             NSInteger hours = (j - 6) * 24; 
  104.                             NSDate * date = [NSDate dateWithTimeInterval:hours * 60 * 60 sinceDate:firstDay]; 
  105.                             NSString * chineseDay = [self calculationChinaCalendarWithDate:date dispalyHoliday:YES]; 
  106.                             [self.chineseCalendarDate replaceObjectAtIndex:j - 1 withObject:chineseDay];// 替换农历日期 
  107.                             NSString * noHoliday = [self calculationChinaCalendarWithDate:date dispalyHoliday:NO]; 
  108.                             [self.chineseCalendarDay replaceObjectAtIndex:j - 1 withObject:noHoliday]; 
  109.                         } 
  110.                     } 
  111.                 } 
  112.             } 
  113.             if (self.days == 31) {// 是否为大月 
  114.                 self.isIncreaseHeight = YES; 
  115.             } else { 
  116.                 self.isIncreaseHeight = NO
  117.             } 
  118.             break; 
  119.         case 7:// 周六 
  120.             for (NSInteger i = 1; i = 7) { 
  121.                     [self.calendarDate replaceObjectAtIndex:i - 1 withObject:@(i - 6)]; 
  122.                     for (NSInteger j = 1; j = 7) { 
  123.                             // 向后推算至本月末 
  124.                             NSInteger hours = (j - 7) * 24; 
  125.                             NSDate * date = [NSDate dateWithTimeInterval:hours * 60 * 60 sinceDate:firstDay]; 
  126.                             NSString * chineseDay = [self calculationChinaCalendarWithDate:date dispalyHoliday:YES]; 
  127.                             [self.chineseCalendarDate replaceObjectAtIndex:j - 1 withObject:chineseDay];// 替换农历日期 
  128.                             NSString * noHoliday = [self calculationChinaCalendarWithDate:date dispalyHoliday:NO]; 
  129.                             [self.chineseCalendarDay replaceObjectAtIndex:j - 1 withObject:noHoliday]; 
  130.                         } 
  131.                     } 
  132.                 } 
  133.             } 
  134.             self.isIncreaseHeight = YES; 
  135.             break; 
  136.     } 
  137.  
  • 计算农历日期

由于农历、节假日都是在同一个位置展示,就放到了一个函数里

1.复活节采用了Meeus/Jones/Butcher算法

2.二十四节气采用了积日日计算公式F = 365.242 (y – 1900) + 6.2 + 15.22 x - 1.9 sin(0.262 x)

探讨:

这个函数当中24节气的算法在执行当中由于需要对积日进行计算,就需要处理1900-1-0这个基准日的日期转换,由于stringFromDate方法过于耗时,会导致一定的线程卡顿,目前我是将这24个节气根据月份分开来执行,然后使用单例NSDateFormatter来解决这个问题

  1. - (NSDateFormatter *)dateFormatter 
  2.     if (!_dateFormatter) { 
  3.         _dateFormatter = [[NSDateFormatter alloc] init]; 
  4.         [_dateFormatter setDateFormat:@"yyyy-MM-dd"]; 
  5.     } 
  6.     return _dateFormatter; 
  7. - (NSDateFormatter *)strDateFormatter 
  8.     if (!_strDateFormatter) { 
  9.         _strDateFormatter = [[NSDateFormatter alloc] init]; 
  10.         [_strDateFormatter setDateFormat:@"MM-dd"]; 
  11.     } 
  12.     return _strDateFormatter; 
  13. - (NSDate *)baseDate 
  14.     if (!_baseDate) { 
  15.         _baseDate = [self.dateFormatter dateFromString:@"1900-1-1"]; 
  16.     } 
  17.     return _baseDate; 
  1. #pragma mark - 计算农历日期 
  2. - (NSString *)calculationChinaCalendarWithDate:(NSDate *)date dispalyHoliday:(BOOL)display 
  3.     if (isEmpty(date)) { 
  4.         return nil; 
  5.     } 
  6.     NSArray * chineseYears = @[@"甲子", @"乙丑", @"丙寅", @"丁卯", @"戊辰", @"己巳", @"庚午", @"辛未", @"壬申", @"癸酉", @"甲戌", @"乙亥", @"丙子", @"丁丑", @"戊寅", @"己卯", @"庚辰", @"辛己", @"壬午", @"癸未", @"甲申", @"乙酉", @"丙戌", @"丁亥", @"戊子", @"己丑", @"庚寅", @"辛卯", @"壬辰", @"癸巳", @"甲午", @"乙未", @"丙申", @"丁酉", @"戊戌", @"己亥", @"庚子", @"辛丑", @"壬寅", @"癸丑", @"甲辰", @"乙巳", @"丙午", @"丁未", @"戊申", @"己酉", @"庚戌", @"辛亥", @"壬子", @"癸丑", @"甲寅", @"乙卯", @"丙辰", @"丁巳", @"戊午", @"己未", @"庚申", @"辛酉", @"壬戌", @"癸亥"]; 
  7.     NSArray * chineseMonths = @[@"正月", @"二月", @"三月", @"四月", @"五月", @"六月", @"七月", @"八月"
  8.                                 @"九月", @"十月", @"冬月", @"腊月"]; 
  9.     NSArray * chineseDays = @[@"初一", @"初二", @"初三", @"初四", @"初五", @"初六", @"初七", @"初八", @"初九", @"初十", @"十一", @"十二", @"十三", @"十四", @"十五", @"十六", @"十七", @"十八", @"十九", @"廿十", @"廿一", @"廿二", @"廿三", @"廿四", @"廿五", @"廿六", @"廿七", @"廿八", @"廿九", @"三十"]; 
  10.     NSCalendar * localeCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierChinese]; 
  11.     unsigned unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth |  NSCalendarUnitDay; 
  12.     NSDateComponents * localeComp = [localeCalendar components:unitFlags fromDate:date]; 
  13.     self.chineseYear = [chineseYears objectAtIndex:localeComp.year - 1]; 
  14.     NSString * m_str = [chineseMonths objectAtIndex:localeComp.month - 1]; 
  15.     self.chineseMonth = m_str; 
  16.     NSString * d_str = [chineseDays objectAtIndex:localeComp.day - 1]; 
  17.     NSString * chineseCal_str = d_str; 
  18.     // 农历节日 
  19.     if([chineseMonths containsObject:m_str] && [d_str isEqualToString:@"初一"]) { 
  20.         chineseCal_str = m_str; 
  21.         if ([m_str isEqualToString:@"正月"] && [d_str isEqualToString:@"初一"]) { 
  22.             chineseCal_str = @"春节"
  23.         } else
  24.             chineseCal_str = @"初一"
  25.         } 
  26.     } else if ([m_str isEqualToString:@"正月"] && [d_str isEqualToString:@"十五"]) { 
  27.         chineseCal_str = @"元宵节"
  28.     } else if ([m_str isEqualToString:@"五月"] && [d_str isEqualToString:@"初五"]) { 
  29.         chineseCal_str = @"端午节"
  30.     } else if ([m_str isEqualToString:@"七月"] && [d_str isEqualToString:@"初七"]) { 
  31.         chineseCal_str = @"七夕"
  32.     } else if ([m_str isEqualToString:@"七月"] && [d_str isEqualToString:@"十五"]) { 
  33.         chineseCal_str = @"中元节"
  34.     } else if ([m_str isEqualToString:@"八月"] && [d_str isEqualToString:@"十五"]) { 
  35.         chineseCal_str = @"中秋节"
  36.     } else if ([m_str isEqualToString:@"九月"] && [d_str isEqualToString:@"初九"]) { 
  37.         chineseCal_str = @"重阳节"
  38.     } else if ([m_str isEqualToString:@"腊月"] && [d_str isEqualToString:@"初八"]) { 
  39.         chineseCal_str = @"腊八节"
  40.     } else if ([m_str isEqualToString:@"腊月"] && [d_str isEqualToString:@"廿三"]) { 
  41.         chineseCal_str = @"小年"
  42.     } else if ([m_str isEqualToString:@"腊月"] && [d_str isEqualToString:@"三十"]) { 
  43.         chineseCal_str = @"除夕"
  44.     } 
  45.     // 公历节日 
  46.     NSDictionary * Holidays = @{@"01-01":@"元旦"
  47.                                 @"02-14":@"情人节"
  48.                                 @"03-08":@"妇女节"
  49.                                 @"03-12":@"植树节"
  50.                                 @"04-01":@"愚人节"
  51.                                 @"05-01":@"劳动节"
  52.                                 @"05-04":@"青年节"
  53.                                 @"06-01":@"儿童节"
  54.                                 @"07-01":@"建党节"
  55.                                 @"08-01":@"建军节"
  56.                                 @"09-10":@"教师节"
  57.                                 @"10-01":@"国庆节"
  58.                                 @"12-24":@"平安夜"
  59.                                 @"12-25":@"圣诞节"}; 
  60. //    NSDateFormatter * dateFormatt= [[NSDateFormatter alloc] init]; 
  61. //    [dateFormatt setDateFormat:@"MM-dd"]; 
  62.     NSString * nowStr = [self.strDateFormatter stringFromDate:date]; 
  63.     // 复活节, Meeus/Jones/Butcher算法 
  64.     NSUInteger a = self.year % 19; 
  65.     NSUInteger b = self.year / 100; 
  66.     NSUInteger c = self.year % 100; 
  67.     NSUInteger d = b / 4; 
  68.     NSUInteger e = b % 4; 
  69.     NSUInteger f = (b + 8) / 25; 
  70.     NSUInteger g = (b - f + 1) / 3; 
  71.     NSUInteger h = (19 * a + b - d - g + 15) % 30; 
  72.     NSUInteger i = c / 4; 
  73.     NSUInteger k = c % 4; 
  74.     NSUInteger l = (32 + (2 * e) + (2 * i) - h - k) % 7; 
  75.     NSUInteger m = (a + (11 * h) + (22 * l)) / 451; 
  76.     NSUInteger theMonth = (h + l - (7 * m) + 114) / 31; 
  77.     NSUInteger day = ((h + l - (7 * m) + 114) % 31)+ 1; 
  78.     NSString * easter = [NSString stringWithFormat:@"0%@-%@", @(theMonth), @(day)]; 
  79.     if ([easter isEqualToString:nowStr]) { 
  80.         chineseCal_str = @"复活节"
  81.     } 
  82.     NSArray * array = [Holidays allKeys]; 
  83.     if([array containsObject:nowStr]) { 
  84.         chineseCal_str = [Holidays objectForKey:nowStr]; 
  85.     } 
  86.     // 公历礼拜节日 
  87.     NSCalendar * calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian]; 
  88.     NSDateComponents * comps = [[NSDateComponents alloc] init]; 
  89.     NSInteger unit = NSCalendarUnitDay | NSCalendarUnitWeekday | NSCalendarUnitMonth | NSCalendarUnitYear; 
  90.     comps = [calendar components:unit fromDate:date]; 
  91.     NSUInteger month = [comps month]; 
  92.     NSUInteger dayInMonth = [comps day]; 
  93.     switch (month) { 
  94.         case 5: 
  95.             if (dayInMonth == 14) { 
  96.                 chineseCal_str = @"母亲节"
  97.             } 
  98.             break; 
  99.         case 6: 
  100.             if (dayInMonth == 21) { 
  101.                 chineseCal_str = @"父亲节"
  102.             } 
  103.             break; 
  104.         case 11: 
  105.             if (dayInMonth == 26) { 
  106.                 chineseCal_str = @"感恩节"
  107.             } 
  108.             break; 
  109.         default
  110.             break; 
  111.     } 
  112.     // 二十四节气, 将节气按月份拆开计算,否则由于计算积日所需日期转换stringFromDate方法过于耗时将会造成线程卡顿 
  113.     NSString * solarTerms = @""
  114.     switch (self.month) {// 过滤月份 
  115.         case 1: 
  116.             for (NSInteger i = 0; i < 2; i ++) { 
  117.                 solarTerms = [self calculationSolarTermsWithYear:self.year solarTermsIndex:i]; 
  118.                 switch (i) { 
  119.                     case 0: 
  120.                         if ([solarTerms isEqualToString:nowStr]) { 
  121.                             chineseCal_str = @"小寒"
  122.                         } 
  123.                         break; 
  124.                     case 1: 
  125.                         if ([solarTerms isEqualToString:nowStr]) { 
  126.                             chineseCal_str = @"大寒"
  127.                         } 
  128.                         break; 
  129.                 } 
  130.             } 
  131.             break; 
  132.         case 2: 
  133.             for (NSInteger i = 2; i < 4; i ++) { 
  134.                 solarTerms = [self calculationSolarTermsWithYear:self.year solarTermsIndex:i]; 
  135.                 switch (i) { 
  136.                     case 2: 
  137.                         if ([solarTerms isEqualToString:nowStr]) { 
  138.                             chineseCal_str = @"立春"
  139.                         } 
  140.                         break; 
  141.                     case 3: 
  142.                         if ([solarTerms isEqualToString:nowStr]) { 
  143.                             chineseCal_str = @"雨水"
  144.                         } 
  145.                         break; 
  146.                 } 
  147.             } 
  148.             break; 
  149.         case 3: 
  150.             for (NSInteger i = 4; i < 6; i ++) { 
  151.                 solarTerms = [self calculationSolarTermsWithYear:self.year solarTermsIndex:i]; 
  152.                 switch (i) { 
  153.                     case 4: 
  154.                         if ([solarTerms isEqualToString:nowStr]) { 
  155.                             chineseCal_str = @"惊蛰"
  156.                         } 
  157.                         break; 
  158.                     case 5: 
  159.                         if ([solarTerms isEqualToString:nowStr]) { 
  160.                             chineseCal_str = @"春分"
  161.                         } 
  162.                         break; 
  163.                 } 
  164.             } 
  165.             break; 
  166.         case 4: 
  167.             for (NSInteger i = 6; i < 8; i ++) { 
  168.                 solarTerms = [self calculationSolarTermsWithYear:self.year solarTermsIndex:i]; 
  169.                 switch (i) { 
  170.                     case 6: 
  171.                         if ([solarTerms isEqualToString:nowStr]) { 
  172.                             chineseCal_str = @"清明"
  173.                         } 
  174.                         break; 
  175.                     case 7: 
  176.                         if ([solarTerms isEqualToString:nowStr]) { 
  177.                             chineseCal_str = @"谷雨"
  178.                         } 
  179.                         break; 
  180.                 } 
  181.             } 
  182.             break; 
  183.         case 5: 
  184.             for (NSInteger i = 8; i < 10; i ++) { 
  185.                 solarTerms = [self calculationSolarTermsWithYear:self.year solarTermsIndex:i]; 
  186.                 switch (i) { 
  187.                     case 8: 
  188.                         if ([solarTerms isEqualToString:nowStr]) { 
  189.                             chineseCal_str = @"立夏"
  190.                         } 
  191.                         break; 
  192.                     case 9: 
  193.                         if ([solarTerms isEqualToString:nowStr]) { 
  194.                             chineseCal_str = @"小满"
  195.                         } 
  196.                         break; 
  197.                 } 
  198.             } 
  199.             break; 
  200.         case 6: 
  201.             for (NSInteger i = 10; i < 12; i ++) { 
  202.                 solarTerms = [self calculationSolarTermsWithYear:self.year solarTermsIndex:i]; 
  203.                 switch (i) { 
  204.                     case 10: 
  205.                         if ([solarTerms isEqualToString:nowStr]) { 
  206.                             chineseCal_str = @"芒种"
  207.                         } 
  208.                         break; 
  209.                     case 11: 
  210.                         if ([solarTerms isEqualToString:nowStr]) { 
  211.                             chineseCal_str = @"夏至"
  212.                         } 
  213.                 } 
  214.             } 
  215.             break; 
  216.         case 7: 
  217.             for (NSInteger i = 12; i < 14; i ++) { 
  218.                 solarTerms = [self calculationSolarTermsWithYear:self.year solarTermsIndex:i]; 
  219.                 switch (i) { 
  220.                     case 12: 
  221.                         if ([solarTerms isEqualToString:nowStr]) { 
  222.                             chineseCal_str = @"小暑"
  223.                         } 
  224.                         break; 
  225.                     case 13: 
  226.                         if ([solarTerms isEqualToString:nowStr]) { 
  227.                             chineseCal_str = @"大暑"
  228.                         } 
  229.                     break;                } 
  230.             } 
  231.             break; 
  232.         case 8: 
  233.             for (NSInteger i = 14; i < 16; i ++) { 
  234.                 solarTerms = [self calculationSolarTermsWithYear:self.year solarTermsIndex:i]; 
  235.                 switch (i) { 
  236.                     case 14: 
  237.                         if ([solarTerms isEqualToString:nowStr]) { 
  238.                             chineseCal_str = @"立秋"
  239.                         } 
  240.                         break; 
  241.                     case 15: 
  242.                         if ([solarTerms isEqualToString:nowStr]) { 
  243.                             chineseCal_str = @"处暑"
  244.                         } 
  245.                         break; 
  246.                 } 
  247.             } 
  248.             break; 
  249.         case 9: 
  250.             for (NSInteger i = 16; i < 18; i ++) { 
  251.                 solarTerms = [self calculationSolarTermsWithYear:self.year solarTermsIndex:i]; 
  252.                 switch (i) { 
  253.                     case 16: 
  254.                         if ([solarTerms isEqualToString:nowStr]) { 
  255.                             chineseCal_str = @"白露"
  256.                         } 
  257.                         break; 
  258.                     case 17: 
  259.                         if ([solarTerms isEqualToString:nowStr]) { 
  260.                             chineseCal_str = @"秋分"
  261.                         } 
  262.                         break; 
  263.                 } 
  264.             } 
  265.             break; 
  266.         case 10: 
  267.             for (NSInteger i = 18; i < 20; i ++) { 
  268.                 solarTerms = [self calculationSolarTermsWithYear:self.year solarTermsIndex:i]; 
  269.                 switch (i) { 
  270.                     case 18: 
  271.                         if ([solarTerms isEqualToString:nowStr]) { 
  272.                             chineseCal_str = @"寒露"
  273.                         } 
  274.                         break; 
  275.                     case 19: 
  276.                         if ([solarTerms isEqualToString:nowStr]) { 
  277.                             chineseCal_str = @"霜降"
  278.                         } 
  279.                         break; 
  280.                 } 
  281.             } 
  282.             break; 
  283.         case 11: 
  284.             for (NSInteger i = 20; i < 22; i ++) { 
  285.                 solarTerms = [self calculationSolarTermsWithYear:self.year solarTermsIndex:i]; 
  286.                 switch (i) { 
  287.                     case 20: 
  288.                         if ([solarTerms isEqualToString:nowStr]) { 
  289.                             chineseCal_str = @"立冬"
  290.                         } 
  291.                         break; 
  292.                     case 21: 
  293.                         if ([solarTerms isEqualToString:nowStr]) { 
  294.                             chineseCal_str = @"小雪"
  295.                         } 
  296.                     break; 
  297.                 } 
  298.             } 
  299.             break; 
  300.         case 12: 
  301.             for (NSInteger i = 22; i < 24; i ++) { 
  302.                 solarTerms = [self calculationSolarTermsWithYear:self.year solarTermsIndex:i]; 
  303.                 switch (i) { 
  304.                     case 22: 
  305.                         if ([solarTerms isEqualToString:nowStr]) { 
  306.                             chineseCal_str = @"大雪"
  307.                         } 
  308.                         break; 
  309.                     case 23: 
  310.                         if ([solarTerms isEqualToString:nowStr]) { 
  311.                             chineseCal_str = @"冬至"
  312.                         } 
  313.                         break; 
  314.                 } 
  315.             } 
  316.             break; 
  317.     } 
  318.     if (display == YES) {// 需要显示假期&节日 
  319.         return chineseCal_str; 
  320.     } 
  321.     return d_str; 
  • 计算24节气的具体日期

这里的计算是整个线程里最耗时的地方,昨天用instruments查看这里的执行,竟然有8000x,我想最可能到这这个的原因就是dateFromString这里了,在我做了一些优化调整后,虽然已经不卡顿了,但不知道有什么更好的解决方案吗?

  1. #pragma mark - 计算二十四节气的具体日期 
  2. /** 
  3.  * @param year 年份 
  4.  * @param index 节气索引,0代表小寒,1代表大寒,其它节气按照顺序类推 
  5.  */ 
  6. - (NSString *)calculationSolarTermsWithYear:(NSUInteger)year solarTermsIndex:(NSUInteger)index 
  7.     NSString * solarTerms = @""
  8.     CGFloat base = 365.242 * (year - 1900) + 6.2 + (15.22 * index) - (1.9 * sinf(0.262 * index));// 计算积日 
  9.     NSInteger hours = (base - 1) * 24;// 由于基准日为1900年1月0日,所以这里需要-1 
  10.     NSDate * date = [NSDate dateWithTimeInterval:hours * 60 * 60 sinceDate:self.baseDate]; 
  11.     solarTerms = [self.strDateFormatter stringFromDate:date]; 
  12.     return solarTerms; 
  13.  

2.3 动画

  • 动画方面主要就是两个方面,翻页动画和点击效果
  • 翻页动画 
  1. + (void)animationWithView:(UIView *)view andEffect:(SK_ANIMATION)effect isNext:(BOOL)next 
  2.     CATransition * transition = [CATransition animation]; 
  3.     if (next == YES) {// 向下翻页 
  4.         switch (effect) { 
  5.             case SK_ANIMATION_REVEAL: 
  6.                 transition.type = @"pageUnCurl"
  7.                 transition.subtype = kCATransitionFromLeft; 
  8.                 break; 
  9.             case SK_ANIMATION_RIPPLE: 
  10.                 transition.type = @"rippleEffect"
  11.                 transition.subtype = kCATransitionFromLeft; 
  12.                 break; 
  13.             case SK_ANIMATION_SUCK: 
  14.                 transition.type = @"suckEffect"
  15.                 transition.subtype = kCATransitionFromLeft; 
  16.                 break; 
  17.         } 
  18.     } else { 
  19.         switch (effect) { 
  20.             case SK_ANIMATION_REVEAL: 
  21.                 transition.type = @"pageCurl"
  22.                 transition.subtype = kCATransitionFromLeft; 
  23.                 break; 
  24.             case SK_ANIMATION_RIPPLE: 
  25.                 transition.type = @"rippleEffect"
  26.                 transition.subtype = kCATransitionFromRight; 
  27.                 break; 
  28.             case SK_ANIMATION_SUCK: 
  29.                 transition.type = @"suckEffect"
  30.                 transition.subtype = kCATransitionFromRight; 
  31.                 break; 
  32.         } 
  33.     } 
  34.     transition.duration = 0.5; 
  35.     [view.layer addAnimation:transition forKey:nil]; 
  36.  
  • 点击效果
  1. + (void)clickEffectAnimationForView:(UIView *)view 
  2.     CABasicAnimation * scaleAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale"]; 
  3.     scaleAnimation.fromValue = [NSNumber numberWithFloat:1.3]; 
  4.     scaleAnimation.toValue = [NSNumber numberWithFloat:0.7]; 
  5.     scaleAnimation.duration = 0.1; 
  6.     scaleAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]; 
  7.     [view.layer addAnimation:scaleAnimation forKey:nil]; 
  8.  
责任编辑:庞桂玉 来源: CocoaChina
相关推荐

2023-10-20 17:53:05

2023-03-17 16:44:16

AI绘画

2016-03-23 10:35:31

交互可控干货

2022-05-25 11:24:25

CalendarNutUI移动端

2020-12-29 08:04:16

可视化地图组件日历组件

2012-04-03 13:46:28

2020-02-20 14:40:53

Vim邮件开源

2010-11-18 17:08:44

Oracle使用SQL

2011-01-21 17:00:49

Thunderbird日历

2015-02-11 17:57:36

iOS源码年日历

2021-01-14 05:14:59

JAVA组件同步

2009-12-31 16:50:02

Silverlight

2019-12-12 20:26:11

物联网数据物流

2023-06-13 09:33:37

视频阿里巴巴

2009-09-14 19:21:36

Javascript透

2013-03-22 09:51:36

IP网关网络迁移VoIP

2011-11-30 22:05:03

ibmdw云计算

2021-05-04 18:28:23

Apache KafkSigNoz开源

2012-09-26 16:32:29

点赞
收藏

51CTO技术栈公众号