6种时间选择方式:
年月日、年月日时、年月日时分、时分、日时分、月日时分
调用方式
[[DatePicker shareManager]showWithType:PickerTypeDay title:nil time:@"2018:10:31" backTime:^(NSString * _Nonnull backTimeStr) {
NSLog(@"%@",backTimeStr);
}];
DatePicker.h
typedef NS_ENUM (NSInteger ,PickerType){
PickerTypeDay = 3,//年月日
PickerTypeHour,//年月日时
PickerTypeMinute,//年月日时分
PickerTypeMinuteHour,//时分
PickerTypeMinuteDay,//日时分
PickerTypeMinuteMonth,//月日时分
};
@interface DatePicker : UIView
+(instancetype)shareManager;
-(instancetype)init;
/*
pickerType 时间选择器类型
title 标题(可不传)
time 传入的值会在选择器中选中展示(可不传)
backTime 返回时间(例:yyyy:MM:dd:HH:mm)
*/
-(void)showWithType:(PickerType)pickerType title:(NSString * _Nullable)title time:(NSString * _Nullable )timeStr backTime:(void (^)(NSString * backTimeStr))backTime;
@end
DatePicker.m
#import "DatePicker.h"
#define WIDTH [UIScreen mainScreen].bounds.size.width
#define HEIGHT [UIScreen mainScreen].bounds.size.height
#define KEYWINDOW [UIApplication sharedApplication].keyWindow
@interface DatePicker ()<UIPickerViewDelegate,UIPickerViewDataSource>
@property (nonatomic,strong)UIView * bgView;//背景
@property (nonatomic,strong)UILabel * titlelabel;//标题
@property (nonatomic,strong)UIPickerView * pickerView;//选择器
@property (nonatomic,assign)PickerType pickerType;//类型
@property (nonatomic,strong)NSDateComponents * components;//时间结构体、可计算时间间隔、获取年月日时分、
@property (nonatomic,strong)NSMutableArray * dataArray;//数据源
@property (nonatomic,strong)NSMutableArray * selDateArray;
@property (nonatomic,strong)void (^backTime)(NSString * backTimeStr);//回调
@end
@implementation DatePicker
+(instancetype)shareManager{
static DatePicker * manager = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
if (!manager) {
manager = [[DatePicker alloc] init];
}
});
return manager;
}
-(instancetype)init{
if (self = [super init]) {
[self createView];
}
return self;
}
-(void)createView{
self.frame = CGRectMake(0, 0, WIDTH, HEIGHT);
self.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.3];
_dataArray = [[NSMutableArray alloc]init];
_bgView = [[UIView alloc]initWithFrame:CGRectMake((WIDTH-250)/2, HEIGHT/2-124, 250, 200)];
_bgView.backgroundColor = [UIColor whiteColor];
_bgView.layer.cornerRadius = 5;
_titlelabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, _bgView.bounds.size.width, 33)];
_titlelabel.textAlignment = NSTextAlignmentCenter;
[_bgView addSubview:_titlelabel];
_pickerView = [[UIPickerView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(_titlelabel.frame), _bgView.bounds.size.width, _bgView.fram