之前项目要用到时间选择器,要求是返回特定格式时间,并根据时间判断今明后并显示,所以就根据系统自带的UIDatePicker自己动手写了个demo,有需要的朋友阔以拿去用。
没有很复杂的操作,唯一大家不太熟的可能的链式编程,我在这里小试了下还是很方便的。其余就只是用到很简单的继承、封装、类方法的调用、block的使用、时间的转换等,我想大家都能搞定。布局用了第三方的Masonry,为了方便时间格式转换添加了NSDate的扩展。
Demo地址:
码云: https://gitee.com/Leesonpeng/LSPDatePicker
GitHub: https://github.com/leesonp/LSPDatePicker
假如只要显示年月日调用下面的方法就行了:
//导入LSPDatePicker.h直接调用此方法就可返回年月日格式为:@"yyyy-MM-dd",默认是当前时间
[LSPDatePicker showInView:self.view selectedCallBack:^(NSString *date) {
//do something
}];
若要传入初始服务器时间,只需要用初始化一个LSPDatePicker对象接收,并传入时间和相应格式便可:
//初始化LSPDatePicker对象,调用类方法
LSPDatePicker *datePicker = [LSPDatePicker showInView:self.view selectedCallBack:^(NSString *date) {
}];
/*
切记:
dateStyle(参数1,参数2)里的参数1和参数2的格式要统一,不然会造成程序崩溃.
DateTypeHMS为枚举,Date格式。
dateStr为服务器时间字符串,也可是你选择返回的时间。
此处用到了链式编程思想(不懂何为链式编程的童鞋可以自行百度)
*/
datePicker.dateStyle(DateTypeHMS,dateStr);
下面贴上LSPDatePicker的完整代码:
//
// LSPDatePicker.h
// LSPDatePicker
//
// Created by leeson on 2018/3/6.
// Copyright © 2018年 李斯芃 ---> 512523045@qq.com. All rights reserved.
//
#import <UIKit/UIKit.h>
#import "Masonry.h"
#import "NSDate+Ex.h"
#import "NSDate+Search.h"
typedef NS_ENUM(NSUInteger, DateType) {
///yyyy-MM-dd
DateTypeMYD = 0,
///yyyy-MM-dd HH:mm
DateTypeHM = 1,
///yyyy/MM/dd HH:mm:ss
DateTypeHMS = 2,
///yyyy/MM/dd HH:mm EEE
DateTypeWEEK = 3,
};
typedef void(^DateChooseCallBack)(NSString *date);
@interface LSPDatePicker : UIView
@property (copy, nonatomic) DateChooseCallBack dateChooseCallBack;
///MARK: - --- 外部调用API
+ (LSPDatePicker*)showInView:(UIView *)view selectedCallBack:(DateChooseCallBack)callBack;
///MARK: - --- 链式编程扩展方法(传入类型,时间并且要求两者格式一致)
- (LSPDatePicker * (^)(NSInteger dateType,NSString *dateStr))dateStyle;
@end
//
// LSPDatePicker.m
// LSPDatePicker
//
// Created by leeson on 2018/3/6.
// Copyright © 2018年 李斯芃 ---> 512523045@qq.com. All rights reserved.
//
#import "LSPDatePicker.h"
#define TEXT_COLOR COLOR_RGBA(61, 147, 236, 1)
#define COLOR_RGBA(r, g, b, a) [UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:a]
#define Font(size) [UIFont systemFontOfSize:size]
#define DateFormatDic(dateType) @{@0:@"yyyy-MM-dd",@1:@"yyyy-MM-dd HH:mm",@2:@"yyyy/MM/dd HH:mm:ss",@3:@"yyyy/MM/dd HH:mm EEE"}[dateType]
@interface LSPDatePicker ()
@property (strong, nonatomic) UIView *maskView;
@property (strong, nonatomic) UIView *pickerConten;
@property (strong, nonatomic) UIDatePicker *picker;
@property (strong, nonatomic) UIView *toolBar;
@property (strong, nonatomic) UIButton *sure;
@property (strong, nonatomic) UIButton *cancle;
@property (assign, nonatomic) NSInteger dateType;
@end
@implementation LSPDatePicker
- (instancetype)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
self.maskView = [[UIView alloc] init];
self.maskView.backgroundColor = COLOR_RGBA(0, 0, 0, 0.05);
[self addSubview:self.maskView];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideViews)];
[self.maskView addGestureRecognizer:tap];
[self.maskView mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.mas_equalTo(UIEdgeInsetsMake(0, 0, 0, 0));
}];
self.pickerConten = [[UIView alloc] init];
[self addSubview:self.pickerConten];
[self.pickerConten mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.right.bottom.mas_equalTo(0);
make.height.mas_equalTo(220);
}];
self.picker = [[UIDatePicker alloc] init];
self.picker.datePickerMode = UIDatePickerModeDate;
self.picker.date = [NSDate date];
self.picker.backgroundColor = [UIColor whiteColor];
[self.pickerConten addSubview:self.picker];
[self.picker mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.bottom.right.mas_equalTo(0);
make.height.mas_equalTo(190);
}];
self.toolBar = [[UIView alloc] init];
self.toolBar.backgroundColor = [UIColor whiteColor];
[self.pickerConten addSubview:self.toolBar];
[self.toolBar mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.right.mas_equalTo(0);
make.bottom.mas_equalTo(self.picker.mas_top);
make.height.mas_equalTo(30);
}];
self.cancle = [UIButton buttonWithType:UIButtonTypeCustom];
self.cancle.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
self.cancle.titleLabel.font = Font(15);
self.cancle.tag = 1;
[self.cancle setTitleEdgeInsets:(UIEdgeInsets){0, 15, 0, 0}];
[self.cancle setTitleColor:TEXT_COLOR forState:UIControlStateNormal];
[self.cancle setTitle:@"取消" forState:UIControlStateNormal];
[self.pickerConten addSubview:self.cancle];
[self.cancle mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.top.bottom.mas_equalTo(self.toolBar);
make.right.mas_equalTo(self.toolBar.mas_centerX);
}];
self.sure = [UIButton buttonWithType:UIButtonTypeCustom];
self.sure.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
self.sure.titleLabel.font = Font(15);
self.sure.tag = 2;
[self.sure setTitleEdgeInsets:(UIEdgeInsets){0, 0, 0, 15}];
[self.sure setTitleColor:TEXT_COLOR forState:UIControlStateNormal];
[self.sure setTitle:@"确定" forState:UIControlStateNormal];
[self.pickerConten addSubview:self.sure];
[self.sure mas_makeConstraints:^(MASConstraintMaker *make) {
make.right.top.bottom.mas_equalTo(self.toolBar);
make.left.mas_equalTo(self.toolBar.mas_centerX);
}];
[self.cancle addTarget:self action:@selector(buttonDidClick:) forControlEvents:UIControlEventTouchUpInside];
[self.sure addTarget:self action:@selector(buttonDidClick:) forControlEvents:UIControlEventTouchUpInside];
}
return self;
}
+ (LSPDatePicker *)showInView:(UIView *)view selectedCallBack:(DateChooseCallBack)callBack {
LSPDatePicker *datePicker = [[LSPDatePicker alloc] initWithFrame:view.bounds];
datePicker.dateChooseCallBack = callBack;
[view addSubview:datePicker];
datePicker.pickerConten.transform = CGAffineTransformMakeTranslation(0, 220);
datePicker.maskView.alpha = 0;
[UIView animateWithDuration:0.25 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
datePicker.pickerConten.transform = CGAffineTransformIdentity;
datePicker.maskView.alpha = 1;
} completion:^(BOOL finished) {
}];
return datePicker;
}
///MARK: - --- 隐藏视图
- (void)hideViews{
[UIView animateWithDuration:0.25 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
self.pickerConten.transform = CGAffineTransformMakeTranslation(0, 220);
self.maskView.alpha = 0;
} completion:^(BOOL finished) {
[self removeFromSuperview];
}];
}
///MARK: - --- 取消、确定按钮点击事件
- (void)buttonDidClick:(UIButton *)button {
if (button.tag == 1) {
[self hideViews];
}else {
if (self.dateChooseCallBack) {
//返回的时间格式
self.dateChooseCallBack([self.picker.date stringWithFormat:DateFormatDic(@(self.dateType))]);
[self hideViews];
}
}
}
- (LSPDatePicker * (^)(NSInteger dateType,NSString *dateStr))dateStyle{
return ^LSPDatePicker *(NSInteger dateType,NSString *dateStr) {
if (dateType == DateTypeHM || dateType == DateTypeHMS || dateType == DateTypeWEEK) {
self.dateType = dateType;
self.picker.datePickerMode = UIDatePickerModeDateAndTime;
}else if (dateType == DateTypeMYD){
dateStr = [dateStr componentsSeparatedByString:@" "][0];
}
self.picker.date = [NSDate dateWithString:dateStr format:DateFormatDic(@(self.dateType))];
return self;
};
}
@end
若要接入项目还要导入demo中的Masonry.h以及扩展类NSDate+Ex.h、NSDate+Search.h。
demo中我只是针对我个人的需求展示如下格式,所以要别的格式的需要自行修改代码,不然会崩溃。
代码地址:
码云: https://gitee.com/Leesonpeng/LSPDatePicker
GitHub: https://github.com/leesonp/LSPDatePicker
有什么问题下面可以留言讨论,有时间会回复。做的不好的,欢迎指出,共同进步,谢谢。
网友评论