你的位置:首页 > 操作系统

[操作系统]简单日历的实现

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;