自定义日历控件

作者: 摸着石头过河_崖边树 | 来源:发表于2017-05-07 17:11 被阅读441次

    前言#

    日历控件在一些旅游、教育、约车、学习类APP中经常被使用到,但是往往系统的NSCalendar控件都是不能满足我们项目的需要的,所以我们就会想“会不会有什么第三方日历控件能满足我们的需求”,好不容易找到一个第三方了,但是接下来我们又头疼了,第三方可能会有以下几个问题:
    1、第三方日历控件这么多,我们应该用哪个?
    2、第三方控件一般都集成了很多功能,但是我只是用其中一个小小的功能,就要集成他这么多文件?
    3、要是第三方控件遇到版本更新,我们还必须更新,如果有什么bug,那就更加得不偿失了,等等缺点?

    因此,我建议大家对于自己项目所用的日历控件,最好自定义一个匹配自己的项目,那么,怎么自定义呢?

    详情代码请直接下载demo查看:
    直接拖到项目可以使用-自定义日历-LZBCalendar

    怎样自定义一个日历控件####

    一、整体结构分析####

    常规日历控件包括三个部分:
    1.年月份的选择 (下图头部)
    2.星期几 (中间头部)
    3.日期 (底部日期)


    常用日历结构.png

    二、分析日历应该怎么做####

    1、头部比较简单,就是点击按钮切换月份,获取年月就可以(一般是获取当前年和当前月)

     - (NSDateComponents *)getDateComponentsFromDate:(NSDate *)date
    
     {
        NSDateComponents *component = [[NSCalendar currentCalendar] components:
                                 (NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay) fromDate:date];
        return component;
      }
    

    2、星期的样式是固定的,这个可以自己设置一个数组,保存日期的风格可以是中文@[@"日",@"一",@"二",@"三",@"四",@"五",@"六"],也可以是English

    3、日期控件采用UICollectionView,但是必须要解决几个问题:
    A、要采用多少个UICollectionViewCell,不同个数的UICollectionViewCell将决定UICollectionView的高度?
    解决:如果知道一个月有多少天,就知道最少需要多少个UICollectionViewCell。

     求出date的这个月有多少天
    
    - (NSInteger)totalDaysThisMonth:(NSDate *)date
     {
       NSRange range = [[NSCalendar currentCalendar] rangeOfUnit:NSCalendarUnitDay inUnit:NSCalendarUnitMonth forDate:date];
       return range.length;
    }
    

    还要知道这个月的第1天是星期几,才能知道前面应该空几个UICollectionViewCell

      求出这个月的第1天使星期几,前面空几个cell
    
     - (NSInteger)firstDayInFirstWeekThisMonth:(NSDate *)date
    {
    NSCalendar *calendar = [NSCalendar currentCalendar];
    [calendar setFirstWeekday:1];
    NSDateComponents *comp = [calendar components:(NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay) fromDate:date];
    comp.day = 1;
    NSDate *firstDayOfMonthDate = [calendar dateFromComponents:comp];
    NSUInteger firstWeekday = [calendar ordinalityOfUnit:NSCalendarUnitWeekday inUnit:NSCalendarUnitWeekOfMonth forDate:firstDayOfMonthDate];
    return firstWeekday-1;
    }
    

    求出前面空几个cell,可以用极限法知道,cell总共可能有多少个

    #define limitation_Low  28
    
    #define limitation_Medium  35
    #define limitation_High  42
    

    B、确定了cell的高度,可以计算出UICollectionView的高度,把高度回调用出去

    //collectinView高度
    
      NSInteger marginDays = [self firstDayInFirstWeekThisMonth:self.style.today];
    NSInteger itemCount = marginDays + [self totalDaysThisMonth:self.style.today];
    CGFloat collectionViewHeight = 0;
    if(itemCount <= limitation_Low)
        collectionViewHeight = limitation_Low/self.style.weekDateDays.count *self.itemHeight;
    else if(itemCount > limitation_Low && itemCount <=limitation_Medium)
        collectionViewHeight = limitation_Medium/self.style.weekDateDays.count *self.itemHeight;
    else
        collectionViewHeight = limitation_High/self.style.weekDateDays.count *self.itemHeight;
    self.collectionView.frame = CGRectMake(0, 0, self.bounds.size.width, collectionViewHeight);
    self.contentView.frame = CGRectMake(0, CGRectGetMaxY(self.headerView.frame), self.bounds.size.width, collectionViewHeight);
    

    三、自定义日历整体设计以及使用####

    1、由于日历的样式设置比较多,建议采用一个单独的类来设置日历的基本样式
    比如:LZBCalendarAppearStyle

    2、自定义UICollectionViewCell,应该包括标题的详细信息label(根据自己的项目需要)

    3、采用datasource数据源方式设置日历的显示样式,如果返回nil,就是显示默认参数

    @property (nonatomic, weak)  id<LZBCalendarDataSource> dataSource;
    
    - (NSString *)calendar:(LZBCalendar *)calendar titleForDate:(NSDate *)date;
    - (NSString *)calendar:(LZBCalendar *)calendar subtitleForDate:(NSDate *)date;
    

    4、采用delegate方式回调响应事件

    - (BOOL)calendar:(LZBCalendar *)calendar shouldSelectDate:(NSDate *)date;
    - (void)calendar:(LZBCalendar *)calendar didSelectDate:(NSDate *)date;
    

    LZBCalendar使用方式##

    1、创建控件

     - (LZBCalendar *)calendar
    
    {
       if(_calendar == nil)
    {
      _calendar = [[LZBCalendar alloc]initWithStyle:self.style];
      _calendar.dataSource = self;
      _calendar.delegate = self;
    }
    return _calendar;
    }
     - (LZBCalendarAppearStyle *)style
    {
      if(_style == nil)
     {
      _style = [[LZBCalendarAppearStyle alloc]init];  //没有设置就是默认样式
      _style.isNeedCustomHeihgt = YES;   //需要自定义cell高度
     }
      return _style;
    }
    

    2、必须实现调用数据源方法,更新日历高度

     - (void)calendar:(LZBCalendar *)calendar layoutCallBackHeight:(CGFloat)height
    
    {
       self.calendar.frame = CGRectMake(0, 100, [UIScreen mainScreen].bounds.size.width, height);
    }
    

    3、数据源更改样式
    - (NSString *)calendar:(LZBCalendar *)calendar titleForDate:(NSDate *)date吗
    { //获得当前月比较
    if([[NSDate date] getDateWithMonth] == [date getDateWithMonth])
    {
    NSInteger result =[[NSDate date] getDateWithDay] -[date getDateWithDay];
    switch (result) {
    case 0:
    return @"今天";
    break;
    case 1:
    return @"昨天";
    break;
    case -1:
    return @"明天";
    break;

            default:
                return nil;
                break;
        }
    }
    else
        return nil;
    }
    - (NSString *)calendar:(LZBCalendar *)calendar subtitleForDate:(NSDate *)date
    {
    NSInteger result = [date getDateWithDay];
    switch (result) {
        case 1:
            return @"10";
            break;
        case 2:
            return @"20";
            break;
        case 3:
            return @"免费";
            break;
        default:
            return nil;
            break;
       }
     }
    

    4、代理响应事件

    - (void)calendar:(LZBCalendar *)calendar didSelectDate:(NSDate *)date
    
    {
    NSLog(@"当前调用的方法:%s------行号:line-%d ",__func__, __LINE__);  
     }
    

    结果演示:


    日历选中演示.png

    注意:直接拖到项目可以使用
    详情代码请直接下载demo查看:
    自定义日历-LZBCalendar

    最后赠言###

    如果觉得文章对您有帮助,不要忘记star哦!😝,star 是对程序猿最大的鼓励!

    相关文章

      网友评论

        本文标题:自定义日历控件

        本文链接:https://www.haomeiwen.com/subject/ofzutxtx.html