iOS开发(OC)——日期选择器

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小时制

    //时区
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值