自定义时间选择器

.H 里面

typedef void (^Selectdate)(NSDate * selectdate);//时间

@interface DPFSelectDatePickerView : UIView

@property (nonatomic, strong) UIDatePicker *datePicker;//时间选择器

@property (nonatomic, strong) NSDate *debtTimeDate;//时间选择器

@property (nonatomic, strong) UIView *LineView;//线

@property (nonatomic, strong) UILabel *titleLabel;//标题label

@property (nonatomic, strong) UIButton *closeButton;//关闭按钮

@property (nonatomic, strong) UIButton *okButton;//确定按钮

@property (nonatomic, copy) Selectdate selectdate;

+ (DPFSelectDatePickerView *)showWithTitle:(NSString *)title

                              selectdate:(Selectdate)selectdate;

@end


.M

@implementation DPFSelectDatePickerView

{

    float alertHeight;//弹框整体高度,默认240

    UIView *alertView;//弹框视图

}

+ (DPFSelectDatePickerView *)showWithTitle:(NSString *)title

                                selectdate:(Selectdate)selectdate{

    DPFSelectDatePickerView *alert = [[DPFSelectDatePickerView alloc] initWithTitle:title selectdate:selectdate];

    return alert;

}

- (instancetype)initWithTitle:(NSString *)title selectdate:(Selectdate)selectdate {

    if (self = [super init]) {

        self.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:.4];

        alertHeight = 240;



        _selectdate = [selectdate copy];


        alertView = [[UIView alloc] init];

        alertView.backgroundColor = [UIColor whiteColor];

        alertView.frame = CGRectMake(0, ([UIScreen mainScreen].bounds.size.height-alertHeight), [UIScreen mainScreen].bounds.size.width, alertHeight);

        [self addSubview:alertView];


        _LineView.frame=CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 1);

        _LineView = [[UILabel alloc] init];

        _LineView.backgroundColor = BDRColor_dddddd;

        [alertView addSubview:_LineView];


        _titleLabel = [[UILabel alloc] init];

        _titleLabel.frame = CGRectMake(0, 1, alertView.frame.size.width, 39);

        _titleLabel.backgroundColor = [UIColor colorWithRed:250/255.0 green:250/255.0 blue:250/255.0 alpha:1];

        _titleLabel.textColor = [UIColor blackColor];

        _titleLabel.font = [UIFont boldSystemFontOfSize:17];

        _titleLabel.textAlignment = NSTextAlignmentCenter;

        self.titleLabel.text = title;

        [alertView addSubview:_titleLabel];


        _closeButton = [UIButton buttonWithType:UIButtonTypeCustom];

        _closeButton.frame = CGRectMake(0, 5, 48, 30);

        [_closeButton setTitle:@"取消" forState:UIControlStateNormal];

        [_closeButton setTitleColor:BDRColor_blue forState:UIControlStateNormal];

        _closeButton.titleLabel.font = [UIFont systemFontOfSize:15];

        [_closeButton addTarget:self action:@selector(closeAction) forControlEvents:UIControlEventTouchUpInside];

        [alertView addSubview:_closeButton];


        _okButton = [UIButton buttonWithType:UIButtonTypeCustom];

        _okButton.frame = CGRectMake([UIScreen mainScreen].bounds.size.width-48, 5,48, 30);

        [_okButton setTitle:@"确定" forState:UIControlStateNormal];

        [_okButton setTitleColor:BDRColor_blue forState:UIControlStateNormal];

        _okButton.titleLabel.font = [UIFont systemFontOfSize:15];

        [_okButton addTarget:self action:@selector(okAction) forControlEvents:UIControlEventTouchUpInside];

        [alertView addSubview:_okButton];


        _datePicker=[[UIDatePicker alloc]init];

        _datePicker.backgroundColor=[UIColor whiteColor];

        _datePicker.datePickerMode=UIDatePickerModeDate;

        _datePicker.maximumDate= [NSDate date];//今天

        NSString *minStr = @"1949-10-01";

        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];

        [dateFormatter setDateFormat:@"yyyy-MM-dd"];

        NSDate *minDate = [dateFormatter dateFromString:minStr];

        _datePicker.minimumDate=minDate;

        [_datePicker addTarget:self action:@selector(datePickerClick:) forControlEvents:UIControlEventValueChanged];

        _datePicker.frame=CGRectMake(0, 40, [UIScreen mainScreen].bounds.size.width, alertHeight-40);

        [alertView addSubview:_datePicker];



        [self show];

    }

    return self;

}

- (void)show {

    self.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height);

    [[UIApplication sharedApplication].keyWindow addSubview:self];


    alertView.alpha = 0.0;

    [UIView animateWithDuration:0.05 animations:^{

        alertView.alpha = 1;

    }];

}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

    UITouch *touch = [touches anyObject];

    CGPoint pt = [touch locationInView:self];

    if (!CGRectContainsPoint([alertView frame], pt)) {

        [self closeAction];

    }

}

-(void)datePickerClick:(id)sender

{

    UIDatePicker* control = (UIDatePicker*)sender;

    _debtTimeDate = control.date;



}

- (void)okAction{

    if (self.selectdate) {

        self.selectdate(_debtTimeDate);

    }

    [self closeAction];

}

- (void)closeAction {

    [UIView animateWithDuration:0.1 animations:^{

        self.alpha = 0;

    } completion:^(BOOL finished) {

        [self removeFromSuperview];

    }];

}

- (void)dealloc {

    //    NSLog(@"SelectAlert被销毁了");

}

你可能感兴趣的:(自定义时间选择器)