ios 日历简单制作和可选择某段特定时间NSCalendar 和NSDate
来源:互联网 发布:linux和windows换行符 编辑:程序博客网 时间:2024/05/23 01:44
最近做项目需要制作一个活动报名的页面。其中需要定制一个简单的日历表来活动报名,大概效果
活动选择时间时根据当前时间之后7天的7天内可以活动报名
#define CURRENT_Date [NSDate date]#import "CalendarVC.h"@interface CalendarVC ()<UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>{ UILabel *_yearLabel; NSDate *_date; NSInteger _isCurrentMouth;// 0 当前月(默认) -1 以前 1 后一个月 -1 后面不可用 NSInteger _day;//当前日子; NSInteger _spaceDay;//当前月份活动天数;}@property (nonatomic, strong) UICollectionView *collectionView;@end@implementation CalendarVC- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. self.view.backgroundColor = [UIColor colorWithWhite:0.95 alpha:1]; UIView *backView = [[UIView alloc] initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, 60)]; backView.backgroundColor = [UIColor colorWithWhite:0.5 alpha:1]; [self.view addSubview:backView]; UIButton *lastButton = [[UIButton alloc] initWithFrame:CGRectMake(5, 2, 80, 30)]; lastButton.titleLabel.font = [UIFont systemFontOfSize:14]; [lastButton setTitle:@"上一月" forState:UIControlStateNormal]; [backView addSubview:lastButton]; [lastButton addTarget:self action:@selector(clcikLastButton) forControlEvents:UIControlEventTouchUpInside]; UIButton *nextButton = [[UIButton alloc] initWithFrame:CGRectMake(self.view.frame.size.width - 85, 2, 80, 30)]; [nextButton setTitle:@"下一月" forState:UIControlStateNormal]; [backView addSubview:nextButton]; nextButton.titleLabel.font = [UIFont systemFontOfSize:14]; [nextButton addTarget:self action:@selector(clickNextButton) forControlEvents:UIControlEventTouchUpInside]; _yearLabel = [[UILabel alloc] initWithFrame:CGRectMake((self.view.frame.size.width - 100 ) / 2, 0, 100, 35)]; _yearLabel.font = [UIFont systemFontOfSize:14]; [backView addSubview:_yearLabel];// _yearLabel.center = backView.center; _date = CURRENT_Date; _yearLabel.text = [NSString stringWithFormat:@"%ld年%ld月",[self yearWithDate:_date],[self monthWithDate:_date] ]; _yearLabel.textColor = [UIColor whiteColor]; _yearLabel.backgroundColor = [UIColor orangeColor]; _yearLabel.textAlignment = NSTextAlignmentCenter; NSArray *array = @[@"日",@"一",@"二",@"三",@"四",@"五",@"六"]; for (NSInteger i = 0; i < 7; i ++) { CGFloat space = self.view.frame.size.width / array.count; UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(space * i, 35, space, 25)]; label.font = [UIFont systemFontOfSize:14]; label.backgroundColor = [UIColor colorWithWhite:0.3 alpha:1]; [backView addSubview:label]; label.textColor = [UIColor whiteColor]; label.text = array[i]; label.textAlignment = NSTextAlignmentCenter; } [self.view addSubview:self.collectionView]; [self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"Cell"]; self.collectionView.delegate = self; self.collectionView.dataSource = self; _isCurrentMouth = 0; _day = [self dayWithDate:CURRENT_Date]; _spaceDay = [self numberOfDaysInCurrentMonthWithDate:CURRENT_Date] - _day;}- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{ return 42;}- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{ UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath]; cell.backgroundColor = [UIColor lightGrayColor]; for (UIView *view in cell.contentView.subviews) { [view removeFromSuperview]; } if (indexPath.row >= [self firstWeekDayInMonthWithDate:_date] && indexPath.row <= ([self numberOfDaysInCurrentMonthWithDate:_date] + [self firstWeekDayInMonthWithDate:_date] - 1)) { UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width / 7, 50)]; [button setTitle:[NSString stringWithFormat:@"%ld", indexPath.row - [self firstWeekDayInMonthWithDate:_date] + 1] forState:UIControlStateNormal]; [cell.contentView addSubview:button]; button.tag = 100 + [[button titleForState:UIControlStateNormal] integerValue]; button.enabled = NO; if (_isCurrentMouth == 0 ) {//当月 if (button.tag > 100 + _day + 6 && button.tag < 100 + _day + 14 ) {//可以选的日期 button.enabled = YES; button.backgroundColor = [UIColor orangeColor]; } } else if (_isCurrentMouth == 1) { if (_spaceDay < 7) { if (button.tag < 100 -_spaceDay + 14 && button.tag >= 100 + 7 - _spaceDay) { button.enabled = YES; button.backgroundColor = [UIColor orangeColor]; } } else { if (button.tag < 100 -_spaceDay + 14) { button.enabled = YES; button.backgroundColor = [UIColor orangeColor]; } } } } return cell;}- (UICollectionView *)collectionView{ if (!_collectionView) { CGFloat wide = self.view.frame.size.width; UICollectionViewFlowLayout *layou = [[UICollectionViewFlowLayout alloc] init]; layou.itemSize = CGSizeMake((wide - 7) / 7, 50); layou.minimumLineSpacing = 1; layou.minimumInteritemSpacing = 1; _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 150, wide, 300) collectionViewLayout:layou]; } return _collectionView;}-(NSInteger )yearWithDate:(NSDate *)date { NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian]; NSDateComponents *components = [gregorian components:NSCalendarUnitYear fromDate:date]; return [components year];}//获取当前的月-(NSInteger )monthWithDate:(NSDate *)date { NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian]; NSDateComponents *components = [gregorian components:NSCalendarUnitMonth fromDate:date]; return [components month];}//获取当前的日-(NSInteger )dayWithDate:(NSDate *)date { NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian]; NSDateComponents *components = [gregorian components:NSCalendarUnitDay fromDate:date]; return [components day];}//获取当前月有多少天- (NSUInteger)numberOfDaysInCurrentMonthWithDate:(NSDate *)date{ // 频繁调用 [NSCalendar currentCalendar] 可能存在性能问题 NSCalendar *canlendar = [NSCalendar currentCalendar]; return [canlendar rangeOfUnit:NSCalendarUnitDay inUnit:NSCalendarUnitMonth forDate:date].length;}//获得当月的第一天为星期几-(NSInteger )firstWeekDayInMonthWithDate:(NSDate *)date { NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian]; [gregorian setFirstWeekday:1]; NSDateComponents *comps = [gregorian components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay fromDate:date]; [comps setDay:1]; NSDate *newDate = [gregorian dateFromComponents:comps]; NSInteger inte = [gregorian ordinalityOfUnit:NSCalendarUnitWeekday inUnit:NSCalendarUnitWeekOfMonth forDate:newDate]; return (inte - 1);}//减去第一周的天数,剩余天数除以7,得到倍数和余数, 获得一个月有几周- (NSUInteger)numberOfWeeksInCurrentMonthWithDate:(NSDate *)date{ NSUInteger weeks = 0; NSUInteger weekday = [self firstWeekDayInMonthWithDate:date]; if (weekday > 0) { weeks += 1; } NSUInteger monthDays = [self numberOfDaysInCurrentMonthWithDate:date]; weeks = weeks + (monthDays - 7 + weekday )/7; if ((monthDays - 7 + weekday) % 7 > 0) { weeks += 1; } return weeks;}- (void)clcikLastButton{ NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian]; NSDateComponents *comps = [gregorian components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay fromDate:_date]; [comps setMonth:([comps month] - 1)]; _date = [gregorian dateFromComponents:comps]; [self setDayStyle];}- (void)clickNextButton{ NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian]; NSDateComponents *comps = [gregorian components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay fromDate:_date]; [comps setMonth:([comps month] + 1)]; _date = [gregorian dateFromComponents:comps]; [self setDayStyle];}#pragma mark -设置活动时间有效性- (void) setDayStyle{ NSDate *date = CURRENT_Date; NSInteger year = [self yearWithDate: date]; NSInteger mouth = [self monthWithDate:date]; NSInteger newYear = [self yearWithDate:_date]; NSInteger newMonth = [self monthWithDate:_date]; if (newYear < year) { _isCurrentMouth = -1; } else if (newYear == year) { if (newMonth == mouth) { _isCurrentMouth = 0; } else if (newMonth == mouth + 1) { _isCurrentMouth = 1; } else { _isCurrentMouth = -1; } } else if (newYear > year && newMonth == 1 && mouth == 12) { if (_spaceDay > 13) { _isCurrentMouth = -1; } else { _isCurrentMouth = 1; } } else { _isCurrentMouth = -1; } NSString *newDate = [NSString stringWithFormat:@"%ld年%ld月",newYear, newMonth]; _yearLabel.text = newDate; [self.collectionView reloadData];}
0 0
- ios 日历简单制作和可选择某段特定时间NSCalendar 和NSDate
- ios NSDate,NSCalendar,制作日历简单实用的方法
- OC_时间NSDate、日历NSCalendar
- NSDate,NSCalendar,制作日历简单实用的方法
- iOS中NSDate和NSCalendar的简单用法
- ios 制作最简单的日历 NSCalendar
- NSDate和NSCalendar的简单应用
- iOS开发中获取上月的起止时间(NSCalendar、NSDateComponents和NSDate的使用)
- 日期和时间 NSDate NSTimeInterval NSCalendar NSDateComponents NSDateFormatter
- 日历 NSDate NSCalendar
- IOS开发 通过NSDateComponents和NSCalendar定义NSDate
- ios开发系列之NSDate,NSDateFormatter,NSDateComponents和NSCalendar
- ios 获取时间--NSCalendar NSDateComponents NSDate
- Foundation框架-06 日期类NSDate和日历类NSCalendar
- iOS:日历和时间
- iOS NSDateFormatter 和 NSDate简单使用 时间戳的转换
- NSDate、NSCalendar和timezone的关系
- nsdate 和 日历
- Jxl Excel 背景颜色列表
- 读史笔记(I) 日光之下
- 外链接
- 图解VS2010打包全过程(转)
- 闭包的7种形式
- ios 日历简单制作和可选择某段特定时间NSCalendar 和NSDate
- git reset soft,hard,mixed之区别深解
- PHP——数据库(删除)
- java类加载器加载机制解析
- Android GC
- Windows多线程同步
- Java多线程(1)
- Android7.0 自定义view开启硬件加速报错
- C++ 对象的内存布局(上)