基本信息
源码名称:objective-c日历可选日期实例
源码大小:0.04M
文件格式:.zip
开发语言:Swift
更新时间:2017-06-15
友情提示:(无需注册或充值,赞助后即可获取资源下载链接)
嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300
本次赞助数额为: 1 元×
微信扫码支付:1 元
×
请留下您的邮箱,我们将在2小时内将文件发到您的邮箱
源码介绍
//
// 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