实例介绍
【实例简介】
【实例截图】
【实例截图】
【核心代码】
// // ViewController.m // DateDemo // // Created by healthmanage on 16/7/8. // Copyright © 2016年 healthmanager. All rights reserved. // #import "ViewController.h" @interface ViewController ()<UICollectionViewDelegate,UICollectionViewDataSource> @property(nonatomic,strong)UILabel *yearAndMonthLabel;//年月UILabel @property(nonatomic,strong)UICollectionView *collectionV; @property(nonatomic,assign)CGFloat itemWidthF;//item的宽 @property(nonatomic,assign)CGFloat itemHeightF;//item的高 @property(nonatomic,strong)NSMutableArray *daysArray;//用来记录不同月份选中的日子 @property(nonatomic,assign)NSInteger weekFirstDayInt;//当前月的第一天是星期几 @property(nonatomic,copy)NSString *monthStr;//某月 @property(nonatomic,copy)NSString *todayMonthStr;//当天年月 @property(nonatomic,assign)NSInteger todayDayInt;//当天日 @end #define f_Device_w [UIScreen mainScreen].bounds.size.width//屏幕宽度 #define Weekdays @[@"日", @"一", @"二", @"三", @"四", @"五", @"六"] @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. _itemWidthF = f_Device_w/7; _itemHeightF = 40; _daysArray = [NSMutableArray new]; //获取当前日期 NSString *dateStr = [self getCurMonthday]; //获取当前年月 _todayMonthStr = [dateStr substringToIndex:6]; //获取当前日 _todayDayInt = [[dateStr substringFromIndex:6] intValue]; //获取当前月的第一天是星期几 _weekFirstDayInt = [self weekdayOfFirstDayInDate:_todayMonthStr]; //获取年月 _monthStr = _todayMonthStr; //显示头视图年月 [self showYearAndMonth]; //显示日历 [self showCollectionView]; } #pragma mark --- 创建头视图左右按钮 -(void)showYearAndMonth { //设置头背景 UIView *headView = [[UIView alloc] initWithFrame:CGRectMake(0, 50, f_Device_w, 75)]; headView.backgroundColor = [UIColor lightGrayColor]; [self.view addSubview:headView]; //创建左边按钮 UIButton *leftBtn = [UIButton buttonWithType:UIButtonTypeCustom]; leftBtn.frame = CGRectMake(10, 3, 30, 39); [leftBtn setBackgroundImage:[UIImage imageNamed:@"left.png"] forState:UIControlStateNormal]; [headView addSubview:leftBtn]; [leftBtn addTarget:self action:@selector(leftBtnClick:) forControlEvents:UIControlEventTouchUpInside]; //创建年月显示UILabel _yearAndMonthLabel = [[UILabel alloc] initWithFrame:CGRectMake(50, 3, f_Device_w-50*2, 39)]; _yearAndMonthLabel.text = [NSString stringWithFormat:@"%@年%@月",[_todayMonthStr substringWithRange:NSMakeRange(0, 4)],[_todayMonthStr substringWithRange:NSMakeRange(4, 2)]]; _yearAndMonthLabel.textAlignment = NSTextAlignmentCenter; _yearAndMonthLabel.textColor = [UIColor lightGrayColor]; _yearAndMonthLabel.backgroundColor = [UIColor whiteColor]; [headView addSubview:_yearAndMonthLabel]; //创建右边按钮 UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeCustom]; rightBtn.frame = CGRectMake(f_Device_w-40, 3, 30, 39); [rightBtn setBackgroundImage:[UIImage imageNamed:@"right.png"] forState:UIControlStateNormal]; [headView addSubview:rightBtn]; [rightBtn addTarget:self action:@selector(rightBtnClick:) forControlEvents:UIControlEventTouchUpInside]; //创建星期文字 for (int i = 0; i < Weekdays.count; i ) { UILabel *weekdayLabel = [[UILabel alloc] initWithFrame:CGRectMake(_itemWidthF*i, 45, _itemWidthF, 29)]; weekdayLabel.textAlignment = NSTextAlignmentCenter; weekdayLabel.text = Weekdays[i]; weekdayLabel.textColor = [UIColor darkGrayColor]; weekdayLabel.backgroundColor = [UIColor whiteColor]; [headView addSubview:weekdayLabel]; } //分割线 UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0, 74, f_Device_w, 1)]; lineView.backgroundColor = [UIColor blackColor]; [headView addSubview:lineView]; } #pragma mark --- 显示日历UICollectionView -(void)showCollectionView { //创建UICollectionView UICollectionViewFlowLayout *flowLayout = [UICollectionViewFlowLayout new]; flowLayout.sectionInset = UIEdgeInsetsZero;//每个分区插入间隔为0 flowLayout.itemSize = CGSizeMake(_itemWidthF, _itemHeightF);//item的大小 flowLayout.minimumLineSpacing = 0;//最小行间距 flowLayout.minimumInteritemSpacing = 0;//最小列间距 _collectionV = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 125, f_Device_w, 240) collectionViewLayout:flowLayout]; _collectionV.delegate = self; _collectionV.dataSource = self; _collectionV.backgroundColor = [UIColor whiteColor]; [self.view addSubview:_collectionV]; //注册Cell [_collectionV registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cell"]; } #pragma mark --- 左按钮点击事件 -(void)leftBtnClick:(UIButton *)buttonN { //NSLog(@"点击了左侧按钮。。。。"); //设置上一个年月 NSString *lastMonth = [self completeMonth:_monthStr gap:-1]; //判断当天日期之前的时间不能选择 if ([lastMonth compare:_todayMonthStr options:NSNumericSearch] != NSOrderedAscending) { _monthStr = lastMonth; _yearAndMonthLabel.text = [NSString stringWithFormat:@"%@年%@月",[lastMonth substringWithRange:NSMakeRange(0, 4)],[lastMonth substringWithRange:NSMakeRange(4, 2)]]; //获取当前月的第一天是星期几 _weekFirstDayInt = [self weekdayOfFirstDayInDate:_monthStr]; [_collectionV removeFromSuperview]; //替换日历 [self showCollectionView]; } } #pragma mark --- 右按钮点击事件 -(void)rightBtnClick:(UIButton *)buttonN { //NSLog(@"点击了右侧按钮。。。。"); //设置下一个月日期 NSString *nextMonth = [self completeMonth:_monthStr gap:1]; _monthStr = nextMonth; _yearAndMonthLabel.text = [NSString stringWithFormat:@"%@年%@月",[nextMonth substringWithRange:NSMakeRange(0, 4)],[nextMonth substringWithRange:NSMakeRange(4, 2)]]; //获取当前月的第一天是星期几 _weekFirstDayInt = [self weekdayOfFirstDayInDate:_monthStr]; [_collectionV removeFromSuperview]; //替换日历 [self showCollectionView]; } #pragma mark -- 根据年月获取当前月的第一天是星期几 - (NSInteger)weekdayOfFirstDayInDate:(NSString *)curYearMonth { NSDateFormatter *dateFormatter = [NSDateFormatter new]; [dateFormatter setDateFormat:@"yyyyMM"]; NSDate *date = [dateFormatter dateFromString:curYearMonth]; NSCalendar *calendar = [NSCalendar currentCalendar]; [calendar setFirstWeekday:1]; NSDateComponents *components = [calendar components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay fromDate:date]; [components setDay:1]; NSDate *firstDate = [calendar dateFromComponents:components]; NSDateComponents *firstComponents = [calendar components:NSCalendarUnitWeekday fromDate:firstDate]; return firstComponents.weekday-1; } #pragma mark --- 获取当前年月日 -(NSString *)getCurMonthday{ NSDate *today = [NSDate date]; NSDateFormatter *dateFormatter = [NSDateFormatter new]; [dateFormatter setDateFormat:@"yyyyMMdd"]; NSString *strDate = [dateFormatter stringFromDate:today]; return strDate; } #pragma mark --- 根据年月获取当前月的总天数 -(NSInteger)totalDaysInMothOfDate:(NSString *)curYearMonth { NSDateFormatter *dateFormatter = [NSDateFormatter new]; [dateFormatter setDateFormat:@"yyyyMM"];//日期形式 NSDate *dateE = [dateFormatter dateFromString:curYearMonth];//将NSString转换成NSDate类型 NSRange rangeE = [[NSCalendar currentCalendar] rangeOfUnit:NSCalendarUnitDay inUnit:NSCalendarUnitMonth forDate:dateE];//参数一:最小单元,参数二:在哪个单元中,参数三:传入日期 return rangeE.length; } #pragma mark --- 根据传值计算间隔月份:传入负值,表示之前的月份,传入正值,表示之后的月份 -(NSString *)completeMonth:(NSString *)month gap:(int) gap{ NSDateFormatter *dateFormatter = [NSDateFormatter new]; [dateFormatter setDateFormat:@"yyyyMM"]; NSDate *monthDate = [dateFormatter dateFromString:month]; NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian]; NSDateComponents *adcomps = [[NSDateComponents alloc] init]; [adcomps setMonth:gap]; NSDate *newdate = [calendar dateByAddingComponents:adcomps toDate:monthDate options:0]; return [dateFormatter stringFromDate:newdate]; } #pragma mark --- UICollectionViewDelegate and DataSource -(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { return _weekFirstDayInt [self totalDaysInMothOfDate:_monthStr]; } -(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath]; UILabel *nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, _itemWidthF, _itemWidthF)]; nameLabel.backgroundColor = [UIColor clearColor]; nameLabel.textAlignment = NSTextAlignmentCenter; nameLabel.font = [UIFont systemFontOfSize:15]; nameLabel.tag = 100; [cell addSubview:nameLabel]; cell.tag = 1; //1.判断这个月的一号是星期几,并对应显示 if (indexPath.row >= _weekFirstDayInt) { int dayInt = indexPath.row 1-_weekFirstDayInt; nameLabel.text = [NSString stringWithFormat:@"%d",dayInt]; //2.根据不同条件显示不同的颜色 //当显示的是未来的月份时,字体显示黑色 if ([_monthStr compare:_todayMonthStr options:NSNumericSearch] == NSOrderedDescending) { nameLabel.textColor = [UIColor blackColor]; } //当显示的是当前月份时 else if([_monthStr compare:_todayMonthStr options:NSNumericSearch] == NSOrderedSame) { //在当前日期之前的时间,字体显示为浅灰色 if (dayInt < _todayDayInt) { nameLabel.textColor = [UIColor lightGrayColor]; } //当前日期,字体显示为红色 else if(dayInt == _todayDayInt) { nameLabel.textColor = [UIColor redColor]; } //当前日期之后的时间,字体显示为黑色 else { nameLabel.textColor = [UIColor blackColor]; } } //当前日期之前的时间,字体显示为浅灰色 else { nameLabel.textColor = [UIColor lightGrayColor]; } } //将已经选择好的进行显示 if (_daysArray.count > 0) { for (int i = 0; i < _daysArray.count; i ) { //当前年月 NSString *monthStr = [_daysArray[i] substringToIndex:6]; if ([monthStr isEqualToString:_monthStr]) { //如果与当前月一样,显示某日选中的情况 int dayInt = [[_daysArray[i] substringFromIndex:6] intValue]; if (dayInt == indexPath.row 1-_weekFirstDayInt) { cell.backgroundColor = [UIColor greenColor]; nameLabel.textColor = [UIColor whiteColor]; cell.tag = 2; } } } } return cell; } -(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath { //标记 BOOL flag = NO;//当前日期前的时间,表示不可选的 if ([_monthStr compare:_todayMonthStr options:NSNumericSearch]==NSOrderedDescending || [_monthStr compare:_todayMonthStr options:NSNumericSearch]==NSOrderedSame) { flag = YES;//表示可选的时间 } //选中的显示不同的颜色,并且记录各个月份选中的日子 UICollectionViewCell *cell = [collectionView cellForItemAtIndexPath:indexPath]; UILabel *dayLabel = (UILabel *)[cell viewWithTag:100]; if (flag) { NSString *dayStr = dayLabel.text; if ([dayStr intValue] < 10) { dayStr = [NSString stringWithFormat:@"0%@",dayStr]; } //取消选中 if (cell.tag != 1) { cell.backgroundColor = [UIColor clearColor]; dayLabel.textColor = [UIColor blackColor]; cell.tag = 1; [_daysArray removeObject:[NSString stringWithFormat:@"%@%@",_monthStr,dayStr]]; } else { //选中 cell.backgroundColor = [UIColor greenColor]; dayLabel.textColor = [UIColor whiteColor]; cell.tag = 2; [_daysArray addObject:[NSString stringWithFormat:@"%@%@",_monthStr,dayStr]]; } } } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end
好例子网口号:伸出你的我的手 — 分享!
相关软件
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
网友评论
我要评论