CalendarCell 使用xib建立
#import <UIKit/UIKit.h>
@interface CalendarCell : UICollectionViewCell
@property(nonatomic,strong)UILabel *dateLabel;
@property(nonatomic,strong)UIButton *dateButton;
@end
#import "CalendarCell.h"
@implementation CalendarCell
-(UILabel *)dateLabel{
if (!_dateLabel) {
_dateLabel = [[UILabel alloc]initWithFrame:self.bounds];
_dateLabel.textAlignment = 1;
_dateLabel.font = [UIFont systemFontOfSize:17];
[self addSubview:_dateLabel];
}
return _dateLabel;
}
-(UIButton *)dateButton{
if (!_dateButton) {
_dateButton = [UIButton buttonWithType:UIButtonTypeCustom];
_dateButton.frame = self.bounds;
_dateButton.titleLabel.textAlignment = 1;
_dateButton.titleLabel.font = [UIFont systemFontOfSize:15];
//self.dateButton.backgroundColor = [UIColor redColor];
[self addSubview:_dateButton];
}
return _dateButton;
}
@end
//CalendarView1
#import <UIKit/UIKit.h>
@interface CalendarView1 : UIView<UICollectionViewDataSource,UICollectionViewDelegate>
@property(nonatomic,strong)NSDate *date;
@property(nonatomic,strong)NSDate *today;
//@property(nonatomic,copy)void(^Block)(NSInteger day,NSInteger month,NSInteger year);
-(void)customInterface;
@end
#import "CalendarView1.h"
#import "CalendarCell.h"
@interface CalendarView1()
@property (weak, nonatomic) IBOutlet UIButton *previousButton;
@property (weak, nonatomic) IBOutlet UIButton *nextButton;
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;
@property(nonatomic,strong)NSArray *weekdayArray;
@end
@implementation CalendarView1
-(void)awakeFromNib{
[self customInterface];
}
-(void)customInterface{
self.weekdayArray = @[@"日",@"一",@"二",@"三",@"四",@"五",@"六"];
//设置每个item的宽和高
CGFloat itemWidth = SWIDTH/7;
CGFloat itemHeight = _collectionView.frame.size.height/7;
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
//设置其边界
layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
//设置每个cell的长宽
layout.itemSize = CGSizeMake(itemWidth, itemHeight);
//设定全局的Cell间距
layout.minimumInteritemSpacing = 0;
//设定全局的行间距
layout.minimumLineSpacing = 0;
_collectionView.dataSource =self;
_collectionView.delegate = self;
_collectionView.showsVerticalScrollIndicator = NO;
[_collectionView registerClass:[CalendarCell class] forCellWithReuseIdentifier:@"cell"];
_collectionView.backgroundColor = [UIColor whiteColor];
[_collectionView setCollectionViewLayout:layout animated:YES];
//[self addSubview:_collectionView];
}
-(void)setDate:(NSDate *)date{
if (_date != date) {
_date = date;
[_titleLabel setText:[NSString stringWithFormat:@"%ld-%.2ld",[self year:date],[self month:date]]];
[_collectionView reloadData];
}
}
#pragma mark - date
//获取日
-(NSInteger)day:(NSDate *)date{
//定义一个时间字段的旗标 指定将会获取到 年,月,日的信息
unsigned unitFlags = NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay;
//获取不同时间字段的信息
NSDateComponents *component = [[NSCalendar currentCalendar] components:unitFlags fromDate:date];
//返回获取到的日
return [component day];
}
//获取月
-(NSInteger)month:(NSDate *)date{
//定义一个时间字段的旗标 指定将会获取到 年,月,日的信息
unsigned unitFlags = NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay;
//获取不同时间字段的信息
NSDateComponents *component = [[NSCalendar currentCalendar] components:unitFlags fromDate:date];
//返回获取到的月
return [component month];
}
//获取年
-(NSInteger)year:(NSDate *)date{
//定义一个时间字段的旗标 指定将会获取到 年,月,日的信息
unsigned unitFlags = NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay;
//获取不同时间字段的信息
NSDateComponents *component = [[NSCalendar currentCalendar] components:unitFlags fromDate:date];
//返回获取到的年
return [component year];
}
//返回一个月的第一天是星期几
-(NSInteger)firstWeekDayInThisMonth:(NSDate *)date{
//取得当前用户的逻辑日历
NSCalendar *calendar = [NSCalendar currentCalendar];
/*
- (void)setFirstWeekday:(NSUInteger)value;
设定每周的第一天从星期几开始,比如:
. 如需设定从星期日开始,则value传入1
. 如需设定从星期一开始,则value传入2
*/
[calendar setFirstWeekday:1];
unsigned unitFlags = NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay;
NSDateComponents *components = [calendar components:unitFlags fromDate:date];
//将components的day设置为1
[components setDay:1];
//使用components 对象包含的年月日的时间字段的信息来创建NSDate
NSDate *firstDayOfMonthDate = [calendar dateFromComponents:components];
/*
方法[NSCalendar ordinalityOfUnit: inUnit: fromDate:]
的ordinalityOfUnit参数为NSWeekdayCalendarUnit,inUnit参数为NSWeekCalendarUnit时,
firstWeekday属性影响它的返回值。具体说明如下:
. 当firstWeekday被指定为星期天(即 = 1)时,它返回的值与星期几对应的数值保持一致。比如:
fromDate传入的参数是星期日,则函数返回1
fromDate传入的参数是星期一,则函数返回2
. 当firstWeekday被指定为其它值时(即 <> 1)时,假设firstWeekday被指定为星期一(即 = 2),那么:
fromDate传入的参数是星期一,则函数返回1
fromDate传入的参数是星期二,则函数返回2
fromDate传入的参数是星期日,则函数返回7
*/
NSUInteger firstWeekDay = [calendar ordinalityOfUnit:NSCalendarUnitWeekday inUnit:NSCalendarUnitWeekOfMonth forDate:firstDayOfMonthDate];
//返回第一天星期几
return firstWeekDay-1;
}
//返回这个月有多少天
-(NSInteger)totalDaysInThisMonth:(NSDate *)date{
/*
(NSRange)rangeOfUnit:(NSCalendarUnit)smaller inUnit:(NSCalendarUnit)larger forDate:(NSDate *)date
返回某个特定时间(date)其对应的小的时间单元(smaller)在大的时间单元(larger)中的范围
要取得2008/11/12在所在月份的日期范围则可以这样调用该方法:
则返回1-31。注: fDate存放了2008/11/12
*/
NSRange totalDaysInMonth = [[NSCalendar currentCalendar]rangeOfUnit:NSCalendarUnitDay inUnit:NSCalendarUnitMonth forDate:date];
return totalDaysInMonth.length;
}
//上个月
-(NSDate *)lastMonth:(NSDate *)date{
NSDateComponents *dateComponents = [[NSDateComponents alloc]init];
dateComponents.month = -1;
NSDate *newsDate = [[NSCalendar currentCalendar]dateByAddingComponents:dateComponents toDate:date options:0];
return newsDate;
}
//下个月
-(NSDate *)nextMonth:(NSDate *)date{
NSDateComponents *dateComponents = [[NSDateComponents alloc]init];
dateComponents.month = +1;
NSDate *newDate = [[NSCalendar currentCalendar]dateByAddingComponents:dateComponents toDate:date options:0];
return newDate;
}
#pragma mark -collectionView delegate
//分为两部分 上面一部分显示星期几 下面显示日期
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
return 2;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
if (section == 0) {
//显示最上面的星期几的数组
return _weekdayArray.count;
}else{
//显示日期
return 42;
}
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
CalendarCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
if (indexPath.section == 0) {
// [cell.dateLabel setText:_weekdayArray[indexPath.row]];
// [cell.dateLabel setTextColor:[UIColor colorWithRed:0 green:0.8 blue:0.61 alpha:1]];
[cell.dateButton setTitle:_weekdayArray[indexPath.row] forState:UIControlStateNormal];
[cell.dateButton setTitleColor:[UIColor colorWithRed:0 green:0.8 blue:0.61 alpha:1] forState:UIControlStateNormal];
}else{
//这个月有多少天
NSInteger daysInThisMonth = [self totalDaysInThisMonth:_date];
//这个月第一天星期几 firstWeekDay从0开始
NSInteger firstWeekDay = [self firstWeekDayInThisMonth:_date];
NSInteger day = 0;
//i从0开始
NSInteger i = indexPath.row;
if (i < firstWeekDay) {
//这个月一号前面的cell全部设为空
[cell.dateButton setTitle:@"" forState:UIControlStateNormal];
}else if (i > firstWeekDay + daysInThisMonth -1){
//超过这个月天数的部分设为空
[cell.dateButton setTitle:@"" forState:UIControlStateNormal];
}else{
day = i - firstWeekDay + 1;
[cell.dateButton setTitle:[NSString stringWithFormat:@"%ld",day] forState:UIControlStateNormal];
if (indexPath.row%6==0||indexPath.row%6==1) {
[cell.dateButton setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];
}
else
[cell.dateButton setTitleColor:[UIColor colorWithRed:0.44 green:0.44 blue:0.44 alpha:1] forState:UIControlStateNormal];
//这个月
if([_today isEqualToDate:_date]){
if (day == [self day:_date]) {//今天的日期设置成蓝色
[cell.dateButton setTitleColor:[UIColor colorWithRed:0.26 green:0.59 blue:0.93 alpha:1] forState:UIControlStateNormal];
}else if(day > [self day:_date]){//在今天之后的日期设置成黑色
[cell.dateButton setTitleColor:[UIColor colorWithRed:0.83 green:0.83 blue:0.83 alpha:1] forState:UIControlStateNormal];
}
else if([_today compare:_date] == NSOrderedAscending){//代表调用compare的日期是位于被比较的日期之前,也就是_today的日期大一些,就是在几天的日期前面面的日期全部设为黑色[UIColor colorWithRed:0.83 green:0.83 blue:0.83 alpha:1]
[cell.dateButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
}//else [UIColor colorWithRed:0.83 green:0.83 blue:0.83 alpha:1]
}//if
}//else
}//else
return cell;
}
//返回这个UICollectionView是否可以被选择
//-(BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath{
// if (indexPath.section == 1) {
// //这个月总共多少天
// NSInteger daysInThisMonth = [self totalDaysInThisMonth:_date];
// //这个月第一天星期几
// NSInteger firstWeekDay = [self firstWeekDayInThisMonth:_date];
// NSInteger day = 0;
// NSInteger i = indexPath.row;
// if (i >= firstWeekDay && i <= firstWeekDay +daysInThisMonth-1) {
// //如果cell的下标在这个月日期所在下标的某一天
// //day 带标是这个月的几号
// day = i - firstWeekDay+1;
// if ([_today isEqualToDate:_date]) {
// if (day <= [self day:_date]) {
// return YES;
// }
// }else if ([_today compare:_date] == NSOrderedDescending) {
// return YES;
// }//else if
// }//if
// }//if
// return NO;
//}
#pragma mark - 点击事件的方法
- (IBAction)previousClick:(id)sender {
[UIView transitionWithView:self duration:0.5 options:UIViewAnimationOptionTransitionCurlDown animations:^{
self.date = [self lastMonth:self.date];
[_collectionView reloadData];
} completion:nil];
}
- (IBAction)nextClick:(id)sender {
[UIView transitionWithView:self duration:0.5 options:UIViewAnimationOptionTransitionCurlUp animations:^{
self.date = [self nextMonth:self.date];
[_collectionView reloadData];
} completion:nil];
}
@end