簡單日曆的實現,簡單日曆實現

來源:互聯網
上載者:User

簡單日曆的實現,簡單日曆實現

1.初始化一些全域變數

1       _gregorian = [[NSCalendar alloc]initWithCalendarIdentifier:NSCalendarIdentifierGregorian];2       _calendarDate = [NSDate date];3       _dayInfoUnits = NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay|NSCalendarUnitHour|NSCalendarUnitMinute|NSCalendarUnitSecond;

2.擷取本月第一天是星期幾和本月的天數

 1    //日期組件對象 2      NSDateComponents *components = [_gregorian components:_dayInfoUnits fromDate:_calendarDate]; 3      //擷取到當月1號的目前時間 4      components.day = 1; 5      NSDate *firstDayOfMonth = [_gregorian dateFromComponents:components]; 6      [_gregorian rangeOfUnit:NSCalendarUnitMonth startDate:&firstDayOfMonth interval:NULL forDate:firstDayOfMonth]; 7      NSDateComponents *comps = [_gregorian components:NSCalendarUnitWeekday fromDate:firstDayOfMonth]; 8      //擷取當月1號的星期,星期六對應的weekDay是7 9      NSInteger weekDayBegin = comps.weekday-1;10     //擷取當月的天數11     NSInteger monthLength = [_gregorian rangeOfUnit:NSCalendarUnitDay inUnit:NSCalendarUnitMonth forDate:_calendarDate].length;

3.建立本月日曆

 1 //建立一個月的按鈕 2     CGFloat buttonX = 0; 3     CGFloat buttonY = 0; 4     for (NSInteger i = 0; i<monthLength; i++) { 5         components.day = i+1; 6         NSInteger row = (i+weekDayBegin)/7; 7         NSInteger col = (i+weekDayBegin)%7; 8         buttonX = _dayWH*col; 9         buttonY = _dayWH*row;10         CalendarButton *button = [self dayButtonWithFrame:CGRectMake(buttonX, buttonY, _dayWH, _dayWH)];11         button.title = [NSString stringWithFormat:@"%ld",components.day];12         button.tag = components.day;13         if (col==0) {14             button.titleColor = [UIColor colorWithRed:206/255.0 green:60/255.0 blue:81/255.0 alpha:1];15         }16         if (button.tag == currentDay) {17             button.titleColor = [UIColor orangeColor];18         }19         [_calendarBtnView addSubview:button];20     }

4.建立需要顯示的上個月的日曆

 1 //上個月的遺留日曆 2     NSDateComponents *previousMonthComponents = [_gregorian components:_dayInfoUnits fromDate:_calendarDate]; 3     previousMonthComponents.month--; 4     NSDate *previousMonthDate = [_gregorian dateFromComponents:previousMonthComponents]; 5     NSRange previousMonthDays = [_gregorian rangeOfUnit:NSCalendarUnitDay inUnit:NSCalendarUnitMonth forDate:previousMonthDate]; 6     NSInteger maxDate = previousMonthDays.length - weekDayBegin; 7     for (NSInteger i = 0; i<weekDayBegin; i++) { 8         previousMonthComponents.day = maxDate+1+i; 9         buttonX = _dayWH*i;10         buttonY = 0;11         CalendarButton *previousCalendar = [self dayButtonWithFrame:CGRectMake(buttonX, buttonY, _dayWH, _dayWH)];12         previousCalendar.title = [NSString stringWithFormat:@"%ld",previousMonthComponents.day];13         previousCalendar.enabled = NO;14         [_calendarBtnView addSubview:previousCalendar];15     }

5.建立需要現實的下個月的日曆

//最後一周有多少天    NSInteger lastWeekDay = (weekDayBegin+monthLength)%7;    //下個月開始日曆    NSDateComponents *nextMonthComponents = [_gregorian components:_dayWH fromDate:_calendarDate];    nextMonthComponents.month++;    NSInteger nextMonthDay = ShowDays-monthLength-weekDayBegin;        for (NSInteger i=lastWeekDay; i<nextMonthDay+lastWeekDay; i++) {        nextMonthComponents.day = i-lastWeekDay+1;        NSInteger row = (i+weekDayBegin+monthLength-lastWeekDay)/7;        NSInteger col = i%7;        buttonX = _dayWH*col;        buttonY = _dayWH*row;        CalendarButton *nextMonthCalendar = [self dayButtonWithFrame:CGRectMake(buttonX, buttonY, _dayWH, _dayWH)];        nextMonthCalendar.title = [NSString stringWithFormat:@"%ld",nextMonthComponents.day];        nextMonthCalendar.enabled = NO;        [_calendarBtnView addSubview:nextMonthCalendar];    }

6.切換至上個月或者下個月只需要將月份+1,-1,再重新建立日曆按鈕即可

    NSDateComponents *components = [_gregorian components:_dayInfoUnits fromDate:_calendarDate];    components.day = 1;    components.month --;    NSDate *prevMonthDate = [_gregorian dateFromComponents:components];    _calendarDate = prevMonthDate;

 

 

 

 

  

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.