主要功能:选择日期
主要功能:操作:左滑动,左上按钮点击 下一月
主要功能:操作:右滑动,右上按钮点击 上一月
主要功能:操作:点击日期,日历消失并返回选择的日期
使用方式:
1: 默认创建显示当前日期的日历
先导入工具 #import “XMPickDataView.h”
然后调initWithPoint方法传参数:显示位置,执行代码块
XMPickDataView *dataPick = [[XMPickDataView alloc] initWithPoint:CGPointMake(100, 100) DidBlock:^(NSString *call) {
//call: 返回点击日历中的日期返回来的时间
NSLog(@"pick Date:%@",call);
}];
// 添加进本控制的self.view即可
[self.view addSubview:dataPick];
2: 根据您传进去的date创建日历
先导入工具 #import “XMPickDataView.h”
然后调initWithPoint方法传参数:显示位置,执行代码块,显示的日历日期
NSDateFormatter *getMonthDateformat = [[NSDateFormatter alloc] init];
[getMonthDateformat setDateFormat:@"YYYY年MM月dd日 hh时mm分ss秒"];
XMPickDataView *dataPick = [[XMPickDataView alloc] initWithPoint:CGPointMake(200, 200) DidBlock:^(NSString *call) {
//call: 返回点击日历中的日期返回来的时间
NSLog(@"pick Date:%@",call);
} date:[getMonthDateformat dateFromString:@"2018年1月1日 1时1分1秒"]];
// 添加进本控制的self.view即可
[self.view addSubview:dataPick];
大体就是上边的那种显示了
实现流程
1.先找两张图片,显示左右切换的日期按钮
2.创建XMDatePicker类继承自UIView
3.定义一个代码块DidBlock,并且添加一个单元格表引用 ,并且提供一个给外界的方法来创建日历
//block是代码块,用来把外界一段代码当参数传进方法执行的
这个block是给外界用的,点击日期 执行外界传进来的一段要执行的代码
collectionView 显示日期的单元表格
point: 显示日历的坐标 (x,y)
didBlock:给外界用的
date:日历显示的日期
4.在实现类里定义一些属性,这些属性是不需要外界主动赋值的
5.实现方法
- (instancetype) initWithPoint:(CGPoint) point DidBlock :(DidBlock) didBlock date:(NSDate *) date
这里的self原型是个UIView
- (instancetype) initWithPoint:(CGPoint) point DidBlock :(DidBlock) didBlock date:(NSDate *) date{
//调用父类的init方法
self = [super init];
//通过传进来的point设置view(x,y) 宽高输入固定值 (328,340) 这里的数值是我计算好的
self.frame = CGRectMake(point.x, point.y, 328, 340);
//圆角半径20
self.layer.cornerRadius = 20.0;
//设置view背景完全没有颜色, 跟透明一样 ,就是看起来好看一点把。
[self setBackgroundColor:[UIColor clearColor]];
//外界传进来的代码块让咱先拿到引用
self.didBlock = didBlock;
//流式布局
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
//初始化日历主要显示控件collectionView 设置坐标和宽高,因为他是咱的子控件 (x,y)是基于咱的(x,y)的
self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(13, 56, 302, 276) collectionViewLayout:layout];
//注册单元格,创建单元格时要用到,因为是纯代码,没有xib文件的
[self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"DatePickCell"];
//设置透明颜色,跟透明一样的颜色
[self.collectionView setBackgroundColor:[UIColor clearColor]];
//让他归属咱
[self addSubview:self.collectionView];
//self.backgroundColor=[UIColor blueColor];
//拿到他的代理
self.collectionView.delegate = self;
//拿到他的数据资源
self.collectionView.dataSource = self;
//刷新
[self.collectionView reloadData];
//这里创建的时间格式 是为了获取当前年份
//科普: Y:年 M:月 d:日
NSDateFormatter *dateYearFormatter = [[NSDateFormatter alloc] init];
[dateYearFormatter setDateFormat:@"YYYY"];
NSDateFormatter *dateMonthFormatter = [[NSDateFormatter alloc] init];
[dateMonthFormatter setDateFormat:@"MM"];
//初始化标题控件titleLabel 设置坐标和宽高,因为他是咱的子控件 (x,y)是基于咱的(x,y)的
self.titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(119, 33, 90, 16)];
//字体大小16 字体苹果苹方
[self.titleLabel setFont:[UIFont fontWithName:@"PingFangSC-Regular" size:16]];
//字体颜色
[self.titleLabel setTextColor:[UIColor colorWithRed:76.0/255.0 green:82.0/255.0 blue:70.0/255.0 alpha:1.0]];
//居中
[self.titleLabel setTextAlignment:NSTextAlignmentCenter];
//让他归属咱
[self addSubview:self.titleLabel];
//获取 年 和 月 拼接字符串 然后给标题赋值 这里的date就是外界传进来的时间
[self.titleLabel setText:[NSString stringWithFormat:@"%@年%@月",[dateYearFormatter stringFromDate:date],[dateMonthFormatter stringFromDate:date]]];
/* **/
return self;
}
6:添加方法
- (NSInteger)getMonthsDay:(NSInteger) months forYears:(NSInteger) years和
- (NSInteger)weekdayWithDate:(NSDate *)date
第一个方法通过年份和月份判断当前月份天数,第二个方法通过日期判断星期几
//第一个方法通过年份和月份判断当前月份天数
- (NSInteger)getMonthsDay:(NSInteger) months forYears:(NSInteger) years{
if ((years % 4 == 0 && years % 100 != 0) || years % 400 == 0) {
//leapMonthsDayList是闰年月天数集合
return [self.leapMonthsDayList[months-1] integerValue];
}else{
//ordinaryMonthsDayList是平年月天数集合
return [self.ordinaryMonthsDayList[months-1] integerValue];
}
}
//通过日期判断星期几
- (NSInteger)weekdayWithDate:(NSDate *<