#import"ViewController.h"
#import"CollectionViewCell.h"
#define screenSize [UIScreen mainScreen].bounds.size
@interfaceViewController()
@property(nonatomic,strong)UICollectionView*collectionView;
@property(nonatomic,strong)NSArray*weekdayArray;
@property(nonatomic,strong)NSDate*date;
@property(nonatomic,strong)NSDate*todayDate;
@property(nonatomic,assign)NSIntegermonthDay;
@property(nonatomic,assign)NSIntegermonth;
@property(nonatomic,assign)NSIntegeryear;
@property(nonatomic,assign)NSIntegerfirstWeekdayInThisMonth;
@property(nonatomic,assign)NSIntegertotaldaysInMonth;
@end
@implementationViewController
- (void)viewDidLoad {
[superviewDidLoad];
UICollectionViewFlowLayout*flowLayout = [[UICollectionViewFlowLayoutalloc]init];
flowLayout.itemSize=CGSizeMake(screenSize.width/7,screenSize.height/7);
flowLayout.minimumInteritemSpacing=0;
flowLayout.minimumLineSpacing=0;
flowLayout.sectionInset=UIEdgeInsetsMake(0,0,0,0);
self.collectionView= [[UICollectionViewalloc]initWithFrame:CGRectMake(0,50,screenSize.width,screenSize.height-50)collectionViewLayout:flowLayout];
[self.collectionViewregisterNib:[UINibnibWithNibName:@"CollectionViewCell"bundle:nil]forCellWithReuseIdentifier:@"cell"];
self.collectionView.delegate=self;
self.collectionView.dataSource=self;
self.collectionView.backgroundColor= [UIColorwhiteColor];
[self.viewaddSubview:self.collectionView];
self.weekdayArray=@[@"日",@"一",@"二",@"三",@"四",@"五",@"六"];
self.date= [NSDatedate];
}
#pragma日历的方法
- (void)setDate:(NSDate*)date {
self.todayDate= date;
NSDateComponents*dateComp = [[NSCalendarcurrentCalendar]components:(NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay)fromDate:date];
NSCalendar*calendar = [NSCalendarcurrentCalendar];
calendar.firstWeekday=1;//1.Sun. 2.Mon. 3.Thes. 4.Wed. 5.Thur. 6.Fri. 7.Sat.
dateComp.day=1;
NSDate*firstDayOfMonthDate = [calendardateFromComponents:dateComp];
NSUIntegerfirstWeekday = [calendarordinalityOfUnit:NSCalendarUnitWeekdayinUnit:NSCalendarUnitWeekOfMonthforDate:firstDayOfMonthDate];
self.firstWeekdayInThisMonth= firstWeekday -1;
NSRangedaysInMonth = [calendarrangeOfUnit:NSCalendarUnitDayinUnit:NSCalendarUnitMonthforDate:date];
self.totaldaysInMonth= daysInMonth.length;
[self.collectionViewreloadData];
}
#pragma collectionView的方法
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView*)collectionView {
return2;
}
- (NSInteger)collectionView:(UICollectionView*)collectionView numberOfItemsInSection:(NSInteger)section {
if(section ==0) {
returnself.weekdayArray.count;
}else{
return42;
}
}
- (UICollectionViewCell*)collectionView:(UICollectionView*)collectionView cellForItemAtIndexPath:(NSIndexPath*)indexPath {
CollectionViewCell*cell = [collectionViewdequeueReusableCellWithReuseIdentifier:@"cell"forIndexPath:indexPath];
if(indexPath.section==0) {
cell.textLabel.text=self.weekdayArray[indexPath.row];
}else{
if(indexPath.row
cell.textLabel.text=@"";
[cellsetUserInteractionEnabled:NO];
}elseif(indexPath.row>self.totaldaysInMonth+self.firstWeekdayInThisMonth-1) {
cell.textLabel.text=@"";
[cellsetUserInteractionEnabled:NO];
}else{
[cell.textLabelsetText:[NSStringstringWithFormat:@"%ld",(long)indexPath.row-self.firstWeekdayInThisMonth+1]];
}
}
returncell;
}
- (IBAction)priviousBtn:(id)sender {
NSDateComponents*dateComp = [NSDateComponentsnew];
dateComp.month= -1;
NSDate*newDate = [[NSCalendarcurrentCalendar]dateByAddingComponents:dateComptoDate:self.todayDateoptions:0];
NSLog(@"%@",newDate);
[UIViewtransitionWithView:self.viewduration:0.5options:UIViewAnimationOptionTransitionCurlUpanimations:^{
self.date= newDate;
}completion:nil];
}
- (IBAction)nextBtn:(id)sender {
NSDateComponents*dateComp = [NSDateComponentsnew];
dateComp.month=1;
NSDate*newDate = [[NSCalendarcurrentCalendar]dateByAddingComponents:dateComptoDate:self.todayDateoptions:0];
NSLog(@"%@",newDate);
[UIViewtransitionWithView:self.viewduration:0.5options:UIViewAnimationOptionTransitionCurlDownanimations:^{
self.date= newDate;
}completion:nil];
}
- (void)didReceiveMemoryWarning {
[superdidReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
Demo下载:https://pan.baidu.com/s/1jHXVe9C