iOS开发交流群:301058503
可以自定义地选择多种格式,有年月日时分、年月日、月日时分、时分
用法:
if(picker==nil){
picker=[[DatePickerView alloc] initWithFrame:self.view.frame];
[picker initWithType:YearMonthDay startYear:birthStartYear endYear:birthEndYear completeBlock:^(NSString *timeStamp) {
//确定返回时间戳
} cancelBlock:^{}];
[self.view addSubview:picker];
}else{
[picker selfShow];
}
具体代码:
1.新建继承UIView的类DatePickerView
2.DatePickerView.h代码
typedef enum{
YearMonthDayHourMinus,//年月日时分
YearMonthDay,//年月日
MonthDayHourMinus,//月日时分
HourMinus,//时分
}DateType;//时间选择器样式
-(void)initWithType:(DateType)type startYear:(NSInteger)sYear endYear:(NSInteger)eYear completeBlock:(void(^)(NSString *timeStamp))complete cancelBlock:(void(^)())cancel;
-(void)selfShow;
3.DatePickerView.m代码
@interface DatePickerView()<UIPickerViewDelegate,UIPickerViewDataSource>
{
UIPickerView *datePicker;
UIView *backgroundView;
//日期存储数组
NSMutableArray *yearArray;
NSMutableArray *monthArray;
NSMutableArray *dayArray;
NSMutableArray *hourArray;
NSMutableArray *minuteArray;
NSString *dateFormatter;
//记录位置
NSInteger yearIndex;
NSInteger monthIndex;
NSInteger dayIndex;
NSInteger hourIndex;
NSInteger minuteIndex;
DateType dateType;
NSInteger start_year;
NSInteger end_year;
}
@property (nonatomic,copy)void (^(sureBlock))(NSString *timeStamp);
@property (nonatomic,copy)void (^(cancelBlock))();
@end
@implementation DatePickerView
//type 时间选择器样式
//sYear 时间选择器开始的年份(为了优化体验,可以根据需要选择)
//eYear 时间选择器结束的年份(为了优化体验,可以根据需要选择)
-(void)initWithType:(DateType)type startYear:(NSInteger)sYear endYear:(NSInteger)eYear completeBlock:(void(^)(NSString *))complete cancelBlock:(void(^)())cancel{
self.backgroundColor = [UIColor clearColor];
dateType = type;
start_year = sYear;
end_year = eYear;
[self initData];
[self initPicker];
if(complete){
self.sureBlock = ^(NSString *timeStamp){
complete(timeStamp);
};
}
if(cancel){
self.cancelBlock = ^(){
cancel();
};
}
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(selfHiden)];
[self addGestureRecognizer:tap];
}
//隐藏
-(void)selfHiden{
[UIView animateWithDuration:0.2 animations:^{
self.backgroundColor = [UIColor clearColor];
backgroundView.frame = CGRectMake(0, self.frame.size.height-1, self.frame.size.width, 290);
} completion:^(BOOL finished) {
self.hidden = YES;
}];
}
//显示
-(void)selfShow{
self.hidden = NO;
[UIView animateWithDuration:0.2 animations:^{
self.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.5];
backgroundView.frame = CGRectMake(0, self.frame.size.height-290, self.frame.size.width, 290);
} completion:nil];
}
-(void)initData{
yearArray = [NSMutableArray array];
monthArray = [NSMutableArray array];
dayArray = [NSMutableArray array];
hourArray = [NSMutableArray array];
minuteArray = [NSMutableArray array];
for(NSInteger i=start_year;i<end_year;i++){
[yearArray addObject:[NSString stringWithFormat:@"%ld年",i]];
}
//获取当前时间戳
NSString *timestamp = [NSString stringWithFormat:@"%ld", (long)[[NSDate date] timeIntervalSince1970]];
NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
[formatter setDateStyle:NSDateFormatterMediumStyle];
[formatter setTimeStyle:NSDateFormatterShortStyle];
[formatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"];//时间形式,大写H为24小时制
//时区