ios 封装万年历

作者: 嘿晴天 | 来源:发表于2017-03-14 23:27 被阅读378次
    Untitled.gif

    心血来潮动手写了个万年历,那么如果要动手封装万年历,你可能会遇到那些问题,
    (1)如果去写某一个的日历
    由着这个思路展开,写某一个月的日历的,要注意的又有两个地方:1. 如何显示每天对应星期几,2 月头,月尾不够天数,用上个月月初 和下个月的月初补全。
    就拿第一点来说如果要想正常显示每天是周几,其实只需要,知道第一天星期几,就可以类推出每天星期几,那要怎么知道第一天星期几呢,oc 是有提供相关的函数的给我们利用,来看看代码

      + (NSUInteger)weeklyOrdinality:(NSDate *)date
    {   
        return [[NSCalendar currentCalendar] ordinalityOfUnit:NSDayCalendarUnit inUnit:NSWeekCalendarUnit forDate:date];
    }
    

    第二点这必须要知道这个月有多少天,上个月有多少天,便可以了,知道这个月第一天星期几,则从他的前一天补上上个月的天数

        float calendarY = 0;
        int calendarX = 0;
        //加上上个月
        for (int i = 1; i < weekday; i++) {
            UILabel * day = [[UILabel alloc]initWithFrame:CGRectMake(w*calendarX, 0, w, h)];
            day.text = [NSString stringWithFormat:@"%lu",daysofLastMonth-(weekday-1)+i];
            day.textAlignment = NSTextAlignmentCenter;
            day.textColor = [UIColor hexColorWithString:@"#45A9FC"];
            day.alpha = 0.5;
            day.font = [UIFont systemFontOfSize:getScaleWidth(30)];
            [dayOfMonthView addSubview:day];
            calendarX++;
        }
        //这个月
        for (int i = 1; i <= daysofMonth; i++) {
            UILabel * day = [[UILabel alloc]initWithFrame:CGRectMake(w*calendarX, calendarY, w, h)];
            day.text = [NSString stringWithFormat:@"%d",i];
            day.textColor = [UIColor hexColorWithString:@"#45A9FC"];
            day.textAlignment = NSTextAlignmentCenter;
            day.font = [UIFont systemFontOfSize:getScaleWidth(30)];
            [dayOfMonthView addSubview:day];
            if (calendarX == 6) {
                calendarY += h;
                calendarX = 0;
            }else{
                calendarX++;
            }
        }
        //补充下个月
        int nextmonthDays = dayOfMonthView.subviews.count%7;
        for (int i = 1; i <= 7-nextmonthDays;i++){
            UILabel * day = [[UILabel alloc]initWithFrame:CGRectMake(w*calendarX, calendarY, w, h)];
            day.textColor = [UIColor hexColorWithString:@"#45A9FC"];
            day.alpha = 0.5;
            day.text = [NSString stringWithFormat:@"%d",i];
            day.textAlignment = NSTextAlignmentCenter;
            day.font = [UIFont systemFontOfSize:getScaleWidth(30)];
            [dayOfMonthView addSubview:day];
            calendarX++;
            
        }
    

    (2)如果实现可以无限的左右滑动(点击上个月和下个月)
    如果实现无线的滑动,如果想要一开始就把所有年份和月份加进来就显然不现实了,所以应该在点击下一个月或者上个月动态生成,下下个月,或者上上个月的日历。
    我这边基本的实现,


    67B0F160-D1B3-455C-A35F-481E6550BDE5.png

    但是往左边滑动的时候要记得调整当前scorllview的 横向滑动坐标,因为当contentoffset.x 等于0 是不可以继续往左滑动,可以继续参考下关键代码

    if (view.tag == 3) {
                    UIView *subView = [self.scorllView viewWithTag:1];
                    [subView removeFromSuperview];
                    UIView *subView1 = [self.scorllView viewWithTag:2];
                    //则删掉最左边日历,在右边加一页
                    subView1.tag = 1;
                    view.tag = 2;
                    NSDate *nextdate = [TDCommonTool getMonthOfDate:date isLastMonth:NO];
                    UIView *nextMonthView = [self setOneMonthView:nextdate];
                    nextMonthView.tag = 3;
                    nextMonthView.x = view.x+Screen_Width;
                    [self.scorllView addSubview:nextMonthView];
                    [self.dateArr addObject:nextdate];
                    [self.dateArr removeObjectAtIndex:0];
                    self.scorllView.contentSize = CGSizeMake(getAbsoluteWdith(nextMonthView), self.scorllView.height);
                    
                }else if(view.tag == 1){
                    UIView *subView = [self.scorllView viewWithTag:2];
                    UIView *subView1 = [self.scorllView viewWithTag:3];
                    [subView1 removeFromSuperview];
                    //则删掉最右边边日历,在左边加一页
                    subView.tag = 3;
                    view.tag = 2;
                    NSDate *lastdate = [TDCommonTool getMonthOfDate:date isLastMonth:YES];
                    UIView *lastMonthView = [self setOneMonthView:lastdate];
                    lastMonthView.tag = 1;
                    
                    [self.scorllView addSubview:lastMonthView];
                    [self.dateArr removeObjectAtIndex:2];
                    [self.dateArr insertObject:lastdate atIndex:0];
                    view.x = Screen_Width;
                    lastMonthView.x = 0;
                    subView.x = Screen_Width*2;
                    [self.scorllView setContentOffset:CGPointMake(Screen_Width, 0)];
                    _scrollX = Screen_Width;
                }
    
    

    demo 地址已上传到,觉得好可以给stars
    https://github.com/heysunnyboy/calendar.git

    相关文章

      网友评论

        本文标题:ios 封装万年历

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