一、申明相关属性
@property (nonatomic , strong) MSUShadowView *shadowView;
@property (nonatomic , strong) MSUDatePopView *popView;
二、相关view初始化
- (MSUShadowView *)shadowView{
if (!_shadowView) {
self.shadowView = [[MSUShadowView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
self.shadowView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.5];
[self.view addSubview:self.shadowView];
}
return _shadowView;
}
- (MSUDatePopView *)popView{
if (!_popView) {
self.popView = [[MSUDatePopView alloc] initWithFrame:CGRectMake(0, HEIGHT-250, WIDTH, 250)];
[self.shadowView addSubview:_popView];
_popView.backgroundColor = HEXCOLOR(0xfbfbfb);
_popView.delegate = self;
_popView.hidden = YES;
}
return _popView;
}
三、MSUShadowView相关代码
1.MSUShadowView.h 中的代码
#import
@interface MSUShadowView : UIView
@end
2.MSUShadowView.m中的代码
#import "MSUShadowView.h"
@implementation MSUShadowView
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
// self.hidden = YES;
for (UIView *view in self.subviews) {
CGPoint point = [[touches anyObject] locationInView:view];
if (![view.layer containsPoint:point]) {
self.hidden = YES;
view.hidden = YES;
// 支付页面 防止点击view移除键盘
[self endEditing:YES];
}
}
}
@end
四、MSUDatePopView 代码
1.MSUDatePopView.h中的代码
#import
@protocol MSUDatePopViewDelegate
- (void)datePopViewCompleteBtnClickWithStartStr:(NSString *)startStr stopStr:(NSString *)stopStr;
@end
@interface MSUDatePopView : UIView
@property (nonatomic , weak) id delegate;
@property (nonatomic , strong) UIButton *startBtn;
@property (nonatomic , strong) UIButton *stopBtn;
@end
2.MSUDatePopView.m中的代码
#import "MSUDatePopView.h"
#import "MSUHUD.h"
//masonry
#define MAS_SHORTHAND
#define MAS_SHORTHAND_GLOBALS
#import "Masonry.h"
#define HEXCOLOR(rgbValue) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]
#define SelfWidth [UIScreen mainScreen].bounds.size.width
@interface MSUDatePopView ()
@property (nonatomic , strong) UIPickerView *pickerView;
@property (nonatomic , strong) NSMutableArray *yearArr;
@property (nonatomic , strong) NSMutableArray *monthArr;
@property (nonatomic , strong) NSMutableArray *dayArr;
@property (nonatomic , assign) NSInteger dayNum;
@property (nonatomic , assign) NSInteger selectedRowYear;
@property (nonatomic , assign) NSInteger selectedRowMonth;
@property (nonatomic , assign) NSInteger selectedRowDay;
//这三个参数放在外面,可供外界调用
@property (nonatomic,copy) NSString *year;
@property (nonatomic,copy) NSString *month;
@property (nonatomic,copy) NSString *day;
@property (nonatomic , copy) NSString *startStr;
@property (nonatomic , copy) NSString *stopStr;
//应该跟新天数了,当月份或年份被选择过或是刚进入为true,需要刷新day
@property (nonatomic,assign) Boolean dayShouldChangeEnable;
@end
@implementation MSUDatePopView
- (instancetype)initWithFrame:(CGRect)frame
{
if (self = [super initWithFrame:frame]) {
[self createView];
}
return self;
}
- (void)createView{
UIView *topView = [[UIView alloc] init];
topView.backgroundColor = HEXCOLOR(0xeff0f2);
[self addSubview:topView];
[topView makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.top).offset(0);
make.left.equalTo(self.left).offset(0);
make.width.equalTo(SelfWidth);
make.height.equalTo(35);
}];
UILabel *seleLab = [[UILabel alloc] init];
seleLab.text = @"选择日期";
seleLab.textColor = [UIColor colorWithRed:244/255.0 green:148/255.0 blue:24/255.0 alpha:1];
seleLab.font = [UIFont systemFontOfSize:14];
[self addSubview:seleLab];
[seleLab makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.top).offset(0);
make.left.equalTo(self.left).offset(14);
make.width.equalTo(100);
make.height.equalTo(35);
}];
UIButton *completeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[completeBtn setTitle:@"完成" forState:UIControlStateNormal];
[completeBtn setTitleColor:[UIColor colorWithRed:244/255.0 green:148/255.0 blue:24/255.0 alpha:1] forState:UIControlStateNormal];
completeBtn.titleLabel.font = [UIFont systemFontOfSize:14];
completeBtn.adjustsImageWhenHighlighted = NO;
[self addSubview:completeBtn];
[completeBtn makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.top).offset(0);
make.right.equalTo(self.right).offset(-14);
make.width.equalTo(30);
make.height.equalTo(35);
}];
[completeBtn addTarget:self action:@selector(completeBtnClick:) forControlEvents:UIControlEventTouchUpInside];
UIView *line1View = [[UIView alloc] init];
line1View.backgroundColor = HEXCOLOR(0xc3c3c3);
[self addSubview:line1View];
[line1View makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.top).offset(0);
make.left.equalTo(self.left).offset(0);
make.width.equalTo(SelfWidth);
make.height.equalTo(1);
}];
UIView *lineView = [[UIView alloc] init];
lineView.backgroundColor = HEXCOLOR(0xc3c3c3);
[self addSubview:lineView];
[lineView makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(seleLab.bottom).offset(-1);
make.left.equalTo(self.left).offset(0);
make.width.equalTo(SelfWidth);
make.height.equalTo(1);
}];
UILabel *midLab = [[UILabel alloc] init];
midLab.textAlignment = NSTextAlignmentCenter;
midLab.text = @"至";
midLab.textColor = HEXCOLOR(0x333333);
midLab.font = [UIFont systemFontOfSize:14];
[self addSubview:midLab];
[midLab makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(lineView.bottom).offset(16.5);
make.left.equalTo(self.left).offset(SelfWidth*0.5-10);
make.width.equalTo(20);
make.height.equalTo(20);
}];
self.pickerView = [[UIPickerView alloc] init];
self.pickerView.dataSource = self;
self.pickerView.delegate = self;
[self addSubview:_pickerView];
[_pickerView makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.top).offset(76.5);
make.centerX.equalTo(self.centerX).offset(0);
make.width.equalTo(SelfWidth-28);
make.height.equalTo(self.frame.size.height-79);
}];
[self getDateOfThisMonment];
self.startBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[_startBtn setTitle:[NSString stringWithFormat:@"%@年%@月%@日",self.yearArr[self.selectedRowYear],self.monthArr[self.selectedRowMonth],self.dayArr[self.selectedRowDay]] forState:UIControlStateNormal];
self.startStr = [NSString stringWithFormat:@"%@%@%@",self.yearArr[self.selectedRowYear],self.monthArr[self.selectedRowMonth],self.dayArr[self.selectedRowDay]];
_startBtn.selected = YES;
[_startBtn setTitleColor:HEXCOLOR(0x333333) forState:UIControlStateNormal];
[_startBtn setTitleColor:HEXCOLOR(0xf49418) forState:UIControlStateSelected];
_startBtn.titleLabel.font = [UIFont systemFontOfSize:14];
_startBtn.backgroundColor = HEXCOLOR(0xffffff);
_startBtn.adjustsImageWhenHighlighted = NO;
_startBtn.layer.cornerRadius = 5;
_startBtn.clipsToBounds = YES;
_startBtn.layer.shouldRasterize = YES;
_startBtn.layer.rasterizationScale = [UIScreen mainScreen].scale;
[self addSubview:_startBtn];
[_startBtn makeConstraints:^(MASConstraintMaker *make) {
make.centerY.equalTo(midLab.centerY).offset(0);
make.right.equalTo(midLab.left).offset(-40);
make.width.equalTo(122);
make.height.equalTo(25);
}];
[_startBtn addTarget:self action:@selector(startBtnClick:) forControlEvents:UIControlEventTouchUpInside];
self.stopBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[_stopBtn setTitle:@"截止日期" forState:UIControlStateNormal];
[_stopBtn setTitleColor:HEXCOLOR(0x333333) forState:UIControlStateNormal];
[_stopBtn setTitleColor:HEXCOLOR(0xf49418) forState:UIControlStateSelected];
_stopBtn.titleLabel.font = [UIFont systemFontOfSize:14];
_stopBtn.backgroundColor = HEXCOLOR(0xffffff);
_stopBtn.adjustsImageWhenHighlighted = NO;
_stopBtn.layer.cornerRadius = 5;
_stopBtn.clipsToBounds = YES;
_stopBtn.layer.shouldRasterize = YES;
_stopBtn.layer.rasterizationScale = [UIScreen mainScreen].scale;
[self addSubview:_stopBtn];
[_stopBtn makeConstraints:^(MASConstraintMaker *make) {
make.centerY.equalTo(midLab.centerY).offset(0);
make.left.equalTo(midLab.right).offset(40);
make.width.equalTo(122);
make.height.equalTo(25);
}];
[_stopBtn addTarget:self action:@selector(stopBtnClick:) forControlEvents:UIControlEventTouchUpInside];
}
#pragma mark - 点击
- (void)startBtnClick:(UIButton *)sender{
self.stopBtn.selected = NO;
self.startBtn.selected = YES;
}
- (void)stopBtnClick:(UIButton *)sender{
self.startBtn.selected = NO;
self.stopBtn.selected = YES;
[_stopBtn setTitle:[NSString stringWithFormat:@"%@年%@月%@日",self.yearArr[self.selectedRowYear],self.monthArr[self.selectedRowMonth],self.dayArr[self.selectedRowDay]] forState:UIControlStateNormal];
self.stopStr = [NSString stringWithFormat:@"%@%@%@",self.yearArr[self.selectedRowYear],self.monthArr[self.selectedRowMonth],self.dayArr[self.selectedRowDay]];
}
- (void)completeBtnClick:(UIButton *)sender{
if (self.stopBtn.titleLabel.text.length > 0 && ([self.startStr integerValue] <= [self.stopStr integerValue])) {
if (self.delegate && [self.delegate respondsToSelector:@selector(datePopViewCompleteBtnClickWithStartStr:stopStr:)]) {
[self.delegate datePopViewCompleteBtnClickWithStartStr:self.startStr stopStr:self.stopStr];
}
} else if (self.stopStr.length == 0){
[MSUHUD showFileWithString:@"请选择截止时间"];
} else if ([self.startStr integerValue] > [self.stopStr integerValue]){
[MSUHUD showFileWithString:@"开始时间大于截止时间"];
}
}
#pragma mark - 初始化
- (NSMutableArray *)yearArr{
if (!_yearArr) {
self.yearArr = [NSMutableArray array];
for (NSInteger i = 2014; i < 2050; i++) {
[self.yearArr addObject:[NSString stringWithFormat:@"%ld",i]];
}
}
return _yearArr;
}
- (NSMutableArray *)monthArr{
if (!_monthArr) {
self.monthArr = [NSMutableArray array];
for (NSInteger i = 1; i <= 12; i++) {
[self.monthArr addObject:[NSString stringWithFormat:@"%ld",i]];
}
}
return _monthArr;
}
- (void)setDaysForMonth:(NSInteger)dayNumber{
self.dayArr = nil;
self.dayArr = [NSMutableArray array];
for (NSInteger i = 1; i <= _dayNum; i++) {
[self.dayArr addObject:[NSString stringWithFormat:@"%ld",i]];
}
}
//获取当期的年月日,并且初始化pickView,及一些其他参数
-(void)getDateOfThisMonment{
//获取当前的年月日
NSCalendar *calendar = [NSCalendar currentCalendar];
NSUInteger unitFlags = NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay;
NSDateComponents *dateComponent = [calendar components:unitFlags fromDate:[NSDate date]];
int year = (int)[dateComponent year];
int month = (int)[dateComponent month];
int day = (int)[dateComponent day];
//我根据月份和年份计算天数
[self calculateDayWithMonth:month andYear:year];
// NSLog(@"aaaaaaa%i,%i,%i",year,month,day);
self.selectedRowYear = (year - 2014);
self.selectedRowMonth = (month - 1);
self.selectedRowDay = (day - 1);
//根据现在的年份和月份,初始化pickerView
[self.pickerView selectRow:_selectedRowYear inComponent:0 animated:YES];
[self.pickerView selectRow:_selectedRowMonth inComponent:1 animated:YES];
[self.pickerView selectRow:_selectedRowDay inComponent:2 animated:YES];
}
#pragma mark - 代理
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
return 3;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
if(component == 0){ //年份行数
return self.yearArr.count;
}
else if(component == 1){ //月份行数
return self.monthArr.count;
}
else{ //天行数
return self.dayArr.count;
}
}
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
if (component ==0) { //年份第row行标题
return [NSString stringWithFormat:@"%@年",[self.yearArr objectAtIndex:row]];
}else if(component == 1){ //月份第row行标题
return [NSString stringWithFormat:@"%@月",[self.monthArr objectAtIndex:row]];
}else{ //天数第row行标题
return [NSString stringWithFormat:@"%@日",[self.dayArr objectAtIndex:row]];
}
}
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
if(component == 0){
//当用户更改年份的时候,为防止闰年和非闰年对2月份天数的影响,_dayShouldChangeEnable = true表明需要重新计算对应月份的天数
_dayShouldChangeEnable = true;
self.year = _yearArr[row];//将选中的月份数赋值给公共的年份参数,以便外面调用
self.selectedRowYear = (int)row; //将row赋值给
[pickerView reloadComponent:0]; //为了选中的row变颜色,需要刷新整行
NSLog(@"---%@",self.year);
if (self.startBtn.selected == YES) {
[_startBtn setTitle:[NSString stringWithFormat:@"%@年%@月%@日",self.year,self.monthArr[self.selectedRowMonth],self.dayArr[self.selectedRowDay]] forState:UIControlStateNormal];
self.startStr = [NSString stringWithFormat:@"%@%@%@",self.year,self.monthArr[self.selectedRowMonth],self.dayArr[self.selectedRowDay]];
} else if (self.stopBtn.selected == YES){
[_stopBtn setTitle:[NSString stringWithFormat:@"%@年%@月%@日",self.year,self.monthArr[self.selectedRowMonth],self.dayArr[self.selectedRowDay]] forState:UIControlStateNormal];
self.stopStr = [NSString stringWithFormat:@"%@%@%@",self.year,self.monthArr[self.selectedRowMonth],self.dayArr[self.selectedRowDay]];
}
}
else if(component==1){
//当用户更改月份的时候,_dayShouldChangeEnable = true表明需要重新计算天数
_dayShouldChangeEnable = true;
self.month = _monthArr[row]; //将选中的月份数赋值给公共的月份参数,以便外面调用
self.selectedRowMonth = (int)row;
[pickerView reloadComponent:1];
NSLog(@"---%@",self.month);
if (self.startBtn.selected == YES) {
[_startBtn setTitle:[NSString stringWithFormat:@"%@年%@月%@日",self.yearArr[self.selectedRowYear],self.month,self.dayArr[self.selectedRowDay]] forState:UIControlStateNormal];
self.startStr = [NSString stringWithFormat:@"%@%@%@",self.yearArr[self.selectedRowYear],self.month,self.dayArr[self.selectedRowDay]];
} else if (self.stopBtn.selected == YES){
[_stopBtn setTitle:[NSString stringWithFormat:@"%@年%@月%@日",self.yearArr[self.selectedRowYear],self.month,self.dayArr[self.selectedRowDay]] forState:UIControlStateNormal];
self.stopStr = [NSString stringWithFormat:@"%@%@%@",self.yearArr[self.selectedRowYear],self.month,self.dayArr[self.selectedRowDay]];
}
}
else{
self.day = _dayArr[row];//将选中的天数赋值给公共的天数参数,以便外面调用
NSLog(@"---------->>>>2");
self.selectedRowDay = (int)row;
[pickerView reloadComponent:2];
NSLog(@"---%@",self.day);
if (self.startBtn.selected == YES) {
[_startBtn setTitle:[NSString stringWithFormat:@"%@年%@月%@日",self.yearArr[self.selectedRowYear],self.monthArr[self.selectedRowMonth],self.day] forState:UIControlStateNormal];
self.startStr = [NSString stringWithFormat:@"%@%@%@",self.yearArr[self.selectedRowYear],self.monthArr[self.selectedRowMonth],self.day];
} else if (self.stopBtn.selected == YES){
[_stopBtn setTitle:[NSString stringWithFormat:@"%@年%@月%@日",self.yearArr[self.selectedRowYear],self.monthArr[self.selectedRowMonth],self.day] forState:UIControlStateNormal];
self.stopStr = [NSString stringWithFormat:@"%@%@%@",self.yearArr[self.selectedRowYear],self.monthArr[self.selectedRowMonth],self.day];
}
}
if(_dayShouldChangeEnable) //若,天数需要更新,那么执行
{
//调用计算天数的函数
[self calculateDayWithMonth:[self.month intValue] andYear:[self.year intValue]];
//由于更新的时候self.selectRowDay很可能大于 天数的最大值,比如self.selectRowDay为31,而天数最大值切换至了29,所以若超出,则需要将selectRowDay重新赋值
if(self.selectedRowDay > _dayNum-1){
self.selectedRowDay = _dayNum-1;
NSLog(@"---------->>>>>%zi<<<<<<",self.selectedRowDay);
}
//重新对天数的那一列进行更新
[pickerView reloadComponent:2];
//false
_dayShouldChangeEnable = false;
}
}
-(UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
UILabel* Label = (UILabel*)view;
if (!Label){
Label = [[UILabel alloc] init];
Label.minimumScaleFactor = 8;
Label.adjustsFontSizeToFitWidth = YES;
[Label setTextAlignment:NSTextAlignmentCenter];
[Label setFont:[UIFont systemFontOfSize:15]];
//当年份列,被选中的行对应变成黄色
if (component==0&&_selectedRowYear==row) {
Label.textColor = [UIColor colorWithRed:244/255.0 green:148/255.0 blue:24/255.0 alpha:1];
}else if (component==1&&_selectedRowMonth==row){
Label.textColor = [UIColor colorWithRed:244/255.0 green:148/255.0 blue:24/255.0 alpha:1];
}else if(component==2&&_selectedRowDay==row){
Label.textColor = [UIColor colorWithRed:244/255.0 green:148/255.0 blue:24/255.0 alpha:1];
}
}
Label.text = [self pickerView:pickerView titleForRow:row forComponent:component];
return Label;
}
//根据month和year计算对应的天数
-(void)calculateDayWithMonth:(int) month andYear:(int) year{
float yearF = [self.year floatValue]/4; //能被4整除的是闰年
float yearI = (int)yearF; //若yearI和yearF不一样,也就是说没有被整除,则不是闰年
//当然以上计算没有包括:能被100整除,但不能被400整除的,不是闰年,因为2000年已过2100年还远....
switch (month) {
case 1:_dayNum = 31; break;
case 2:
if(yearF != yearI){_dayNum = 28;}else{
_dayNum = 29;}break;
case 3:_dayNum = 31;break;
case 4:_dayNum = 30;break;
case 5:_dayNum = 31;break;
case 6:_dayNum = 30;break;
case 7:_dayNum = 31;break;
case 8:_dayNum = 31;break;
case 9:_dayNum = 30;break;
case 10:_dayNum = 31;break;
case 11:_dayNum = 30;break;
case 12:_dayNum = 31;break;
default:_dayNum = 31;break;
}
[self setDaysForMonth:_dayNum]; //此处调用函数,将dayArray重新赋值;
}
@end
五、点击按钮,显示MSUDatePopView
self.shadowView.hidden = NO;
self.popView.hidden = NO;
六、实现MSUDatePopView代理
- (void)datePopViewCompleteBtnClickWithStartStr:(NSString *)startStr stopStr:(NSString *)stopStr{
self.shadowView.hidden = YES;
self.startStr = startStr;
self.stopStr = stopStr;
self.tableView.mj_header = [MSUDIYHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewData2)];
[self.tableView.mj_header beginRefreshing];
}