#import "ViewController.h"
#import "CollectionViewCell.h"
#define screenSize [UIScreen mainScreen].bounds.size
@interface ViewController ()<</span>UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>
@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) NSArray *weekdayArray;
@property (nonatomic, strong) NSDate *date;
@property (nonatomic, strong) NSDate *todayDate;
@property (nonatomic, assign) NSInteger monthDay;
@property (nonatomic, assign) NSInteger month;
@property (nonatomic, assign) NSInteger year;
@property (nonatomic, assign) NSInteger firstWeekdayInThisMonth;
@property (nonatomic, assign) NSInteger totaldaysInMonth;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]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 = [[UICollectionView alloc]initWithFrame:CGRectMake(0,50, screenSize.width, screenSize.height-50) collectionViewLayout:flowLayout];
[self.collectionView registerNib:[UINib nibWithNibName:@"CollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"cell"];
self.collectionView.delegate = self;
self.collectionView.dataSource = self;
self.collectionView.backgroundColor = [UIColor whiteColor];
[self.view addSubview:self.collectionView];
self.weekdayArray = @[@"日",@"一",@"二",@"三",@"四",@"五",@"六"];
self.date = [NSDate date];
}
#pragma 日历的方法
- (void)setDate:(NSDate *)date {
self.todayDate = date;
NSDateComponents *dateComp = [[NSCalendar currentCalendar] components:(NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay) fromDate:date];
NSCalendar *calendar = [NSCalendar currentCalendar];
calendar.firstWeekday = 1 ;//1.Sun. 2.Mon. 3.Thes. 4.Wed. 5.Thur. 6.Fri. 7.Sat.
dateComp.day = 1;
NSDate *firstDayOfMonthDate = [calendar dateFromComponents:dateComp];
NSUInteger firstWeekday = [calendar ordinalityOfUnit:NSCalendarUnitWeekday inUnit:NSCalendarUnitWeekOfMonth forDate:firstDayOfMonthDate];
self.firstWeekdayInThisMonth = firstWeekday -1;
NSRange daysInMonth = [calendar rangeOfUnit:NSCalendarUnitDay inUnit:NSCalendarUnitMonth forDate:date];
self.totaldaysInMonth = daysInMonth.length;
[self.collectionView reloadData];
}
#pragma collectionView的方法
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
return 2;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
if (section == 0) {
return self.weekdayArray.count;
} else {
return 42;
}
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
if (indexPath.section == 0) {
cell.textLabel.text = self.weekdayArray[indexPath.row];
} else {
if (indexPath.row < self.firstWeekdayInThisMonth) {
cell.textLabel.text = @"";
[cell setUserInteractionEnabled:NO];
} else if (indexPath.row > self.totaldaysInMonth + self.firstWeekdayInThisMonth - 1) {
cell.textLabel.text = @"";
[cell setUserInteractionEnabled:NO];
} else {
[cell.textLabel setText:[NSString stringWithFormat:@"%ld",(long)indexPath.row - self.firstWeekdayInThisMonth + 1]];
}
}
return cell;
}
- (IBAction)priviousBtn:(id)sender {
NSDateComponents *dateComp = [NSDateComponents new];
dateComp.month = -1;
NSDate *newDate = [[NSCalendar currentCalendar] dateByAddingComponents:dateComp toDate:self.todayDate options:0];
NSLog(@"%@",newDate);
[UIView transitionWithView:self.view duration:0.5 options:UIViewAnimationOptionTransitionCurlUp animations:^{
self.date = newDate;
} completion:nil];
}
- (IBAction)nextBtn:(id)sender {
NSDateComponents *dateComp = [NSDateComponents new];
dateComp.month = 1;
NSDate *newDate = [[NSCalendar currentCalendar] dateByAddingComponents:dateComp toDate:self.todayDate options:0];
NSLog(@"%@",newDate);
[UIView transitionWithView:self.view duration:0.5 options:UIViewAnimationOptionTransitionCurlDown animations:^{
self.date = newDate;
} completion:nil];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
Demo下载:https://pan.baidu.com/s/1jHXVe9C