IOS添加自定义pickView

做IOS程序开发中,经常遇到选择器,大部分被alertView 和 actionsheet来解决问题,但是当选择的项过多时,就需要使用UIPickView。下面就结合前人的基础上,自己封装以下pickView,以备以后查阅使用:

.h文件中的显示:

@interface DatePickView : UIView

+ (instancetype)showOnView:(UIView *)view;

@property (nonatomic , copy) void (^datePickComplet)(NSString *dateStr); /// 范围选择 回调

@end

.m文件中:

#define hourArr @[@"07",@"08",@"09",@"10",@"11",@"12",@"13",@"14",@"15",@"16",@"17",@"18"]
#define minuteArr @[@"00",@"30"]
@interface DatePickView()

@property (nonatomic, strong) UIView* mask;
@property (nonatomic, strong) UIView* headerContainerV;
@property (nonatomic, strong) UIPickerView *pickView;
@property (nonatomic, assign) NSInteger hourIndex;
@property (nonatomic, assign) NSInteger minuteIndex;

@end

@implementation DatePickView

+ (instancetype)showOnView:(UIView *)view {
    DatePickView *datePickView = [[DatePickView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
    datePickView.mask = [[UIView alloc] initWithFrame:view.bounds];
    datePickView.mask.backgroundColor = [UIColor blackColor];
    datePickView.mask.alpha = 0.3 ;
    [view addSubview:datePickView.mask];
    [view addSubview:datePickView];
    return datePickView;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setupUI];
    }
    return self;
}

- (void)drawRect:(CGRect)rect
{
    [self addTap];
}

- (void)addTap
{
    UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self  action:@selector(hide)];
    [self.mask addGestureRecognizer:tap];
}

- (void)hide
{
    [self removeViewFromSubView];
}


- (void)setupUI {
    self.backgroundColor = [UIColor whiteColor];
    _headerContainerV = [UIView new];
    _headerContainerV.backgroundColor = [UIColor groupTableViewBackgroundColor];
    [self addSubview:_headerContainerV];
    
    [_headerContainerV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.mas_top);
        make.left.equalTo(self.mas_left);
        make.width.equalTo(self.mas_width);
        make.height.equalTo(@45);
    }];
    
    UILabel *titleLabel = [[UILabel alloc] init];
    titleLabel.font = [UIFont systemFontOfSize:16.0f];
    titleLabel.textColor = [UIColor colorWithHexString:@"#333333"];
    titleLabel.text = @"选择结束时间";
    [_headerContainerV addSubview:titleLabel];
    
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(_headerContainerV.mas_centerY);
        make.left.equalTo(_headerContainerV.mas_left).with.offset(20);
    }];
    
    UIButton *clearButton = [[UIButton alloc] init];
    clearButton.titleLabel.font = [UIFont systemFontOfSize:16.0f];
    [clearButton setTitleColor:[UIColor colorWithRed:0/255.0 green:160/255.0 blue:233/255.0 alpha:1] forState:UIControlStateNormal];
    [clearButton setTitle:@"确定" forState:UIControlStateNormal];
    [clearButton addTarget:self action:@selector(clearBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [_headerContainerV addSubview:clearButton];
    
    [clearButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(_headerContainerV.mas_centerY);
        make.right.equalTo(_headerContainerV.mas_right).with.offset(-20);
    }];
    
    
    
    self.pickView = [[UIPickerView alloc] init];
    self.pickView.dataSource = self;
    self.pickView.delegate = self;
    self.pickView.backgroundColor = [UIColor whiteColor];
    [self addSubview:self.pickView];
    
    [self.pickView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.mas_left).with.offset(30);
        make.right.equalTo(self.mas_right).with.offset(-30);
        make.top.equalTo(_headerContainerV.mas_bottom);
        make.bottom.equalTo(self.mas_bottom);
    }];
}

#pragma mark - UIPickerViewDataSource
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    return 3;
}


// returns the # of rows in each component..
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    if(component == 0) {
        return [hourArr count];
    }else if(component == 1) {
        return 1;
    }else {
        return [minuteArr count];
    }
}

#pragma mark - UIPickerViewDelegate
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
    if(component == 0) {
        self.hourIndex = row;
    }else if(component == 2) {
        self.minuteIndex = row;
    }
}
- (nullable NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
    if(component == 0){
        return [hourArr objectAtIndex:row];
    }else if (component == 1){
        return @":";
    }else {
        return [minuteArr objectAtIndex:row];
    }
}


- (void)clearBtnClick:(UIButton *)sender {
    [self removeViewFromSubView];
}

- (void)removeViewFromSubView {
    [UIView animateWithDuration:0.25 animations:^{
        [self.mask removeFromSuperview];
        [self removeFromSuperview];
    } completion:^(BOOL finished) {
        if(self.datePickComplet) {
            self.datePickComplet([NSString stringWithFormat:@"%@:%@",[hourArr objectAtIndex:_hourIndex],[minuteArr objectAtIndex:_minuteIndex]]);
        }
    }];
}



知行办公,专业移动办公平台https://zx.naton.cn/
【总监】十二春秋之,[email protected]
【Master】zelo,[email protected]
【运营】运维艄公,[email protected];****
【产品设计】流浪猫,[email protected]
【体验设计】兜兜,[email protected]
【iOS】淘码小工,[email protected]iMcG33K,[email protected]
【Android】人猿居士,[email protected];思路的顿悟,[email protected]
【java】首席工程师MR_W,[email protected]
【测试】土镜问道,[email protected]
【数据】fox009521,[email protected]

你可能感兴趣的:(IOS添加自定义pickView)