OC 版 定制化 PickerView

CRPickerTimeView组件实现
本文介绍了一个自定义iOS时间选择器组件CRPickerTimeView的实现细节,包括不同类型的显示方式及选择时间后的回调处理。
//
//  CRPickerTimeView.h
//  CRZY
//
//  Created by maochengfang on 2021/3/2.
//

#import <UIKit/UIKit.h>

NS_ASSUME_NONNULL_BEGIN

typedef enum : NSUInteger {
    PickerTimeViewTypeOrderTime,
    PickerTimeViewTypeOther,
} PickerTimeViewType;

typedef void(^TimerPickerDidSelectHandler)(NSString* starTime, NSString* endTime);

@interface CRPickerTimeView : UIView

- (instancetype)initWithFrame:(CGRect)frame andFirstData:(NSArray *)firstData andSecondData:(NSArray *)secondData;

@property (nonatomic, assign) PickerTimeViewType showType;

@property (nonatomic, copy) TimerPickerDidSelectHandler blockDidSelect;


@end

NS_ASSUME_NONNULL_END
//
//  CRPickerTimeView.m
//  CRZY
//
//  Created by maochengfang on 2021/3/2.
//

#import "CRPickerTimeView.h"

@interface CRPickerTimeView ()<UIPickerViewDataSource, UIPickerViewDelegate>

@property (nonatomic, strong) UIPickerView *pickerView;

@property (nonatomic, copy) NSArray *firstDataSource;
@property (nonatomic, copy) NSArray *secondDataSource;

@property (nonatomic, copy) NSString* firstSelItem;
@property (nonatomic, copy) NSString* secondSelItem;

@property (nonatomic, strong) UILabel* leftTimeLab;

@property (nonatomic, strong) UILabel* rightTimeLab;

@end

@implementation CRPickerTimeView

- (instancetype)initWithFrame:(CGRect)frame andFirstData:(NSArray *)firstData andSecondData:(NSArray *)secondData{
    
    if (self = [super initWithFrame:frame]) {
        
        _firstDataSource = firstData.copy;
        _secondDataSource = secondData.copy;
        
        self.firstSelItem = [NSString  stringWithFormat:@"%@",self.firstDataSource[0]];
        self.secondSelItem = [NSString  stringWithFormat:@"%@",self.secondDataSource[0]];
        
        UIView* topView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 48)];
        [self addSubview:topView];
        
        UIButton* cancelBtn = [UIKitFactory buttonWithTitle:@"取消" fontSize:13 target:self action:@selector(cancelBtnClick)];
        cancelBtn.frame = CGRectMake(0, 0, 48, 48);
        [cancelBtn setTitleColor:@"#616161".color forState:UIControlStateNormal];
        [cancelBtn.titleLabel setTextAlignment:NSTextAlignmentCenter];
        
        [topView addSubview:cancelBtn];
        
        self.pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 48, SCREEN_WIDTH, 263)];
        self.pickerView.backgroundColor = [UIColor clearColor];
        self.pickerView.delegate = self;
        self.pickerView.dataSource = self;
        [self addSubview:self.pickerView];
        [self.pickerView reloadAllComponents];
        
        UILabel* strLab = [UIKitFactory labelWithText:@"~" textColor:AppTextBlackColor fontSize:15];
        
        strLab.frame = CGRectMake(-10, 140-23,SCREEN_WIDTH, 30);
        [strLab setTextAlignment:NSTextAlignmentCenter];
        [self.pickerView addSubview:strLab];
        
        UILabel* leftTimeLab = [UIKitFactory labelWithText:@":00" textColor:@"#CDCDCD".color fontSize:18];
        leftTimeLab.frame = CGRectMake(100+10, 140-15, 100, 15);
        [self.pickerView addSubview:leftTimeLab];
        self.leftTimeLab = leftTimeLab;
        
        leftTimeLab.attributedText = [leftTimeLab.text attributedStringWithColor:@"#1D1D1D".color font:[UIFont systemFontOfSize:18] range:NSMakeRange(0, 1)];
        
        UILabel* rightTimeLab = [UIKitFactory labelWithText:@":00" textColor:@"#CDCDCD".color fontSize:18];
        rightTimeLab.frame = CGRectMake(SCREEN_WIDTH - 130, 140-15, 80, 15);
        rightTimeLab.attributedText = leftTimeLab.attributedText;
        self.rightTimeLab = rightTimeLab;
        
        [self.pickerView addSubview:rightTimeLab];
        
        UIView* leftUpView = [UIKitFactory lineViewWithFrame:CGRectMake(leftTimeLab.zy_x - 23, 129+30, 20.5, 1)];
        leftUpView.backgroundColor = @"#1D1D1D".color;
        [self addSubview:leftUpView];
        
        UIView* leftDownView = [UIKitFactory lineViewWithFrame:CGRectMake(leftTimeLab.zy_x - 23, leftUpView.zy_bottom + 40, 20.5, 1)];
        leftDownView.backgroundColor = @"#1D1D1D".color;
        [self addSubview:leftDownView];
        
        
        UIView* rightUpView = [UIKitFactory lineViewWithFrame:CGRectMake(rightTimeLab.zy_x - 23, 129+30, 20.5, 1)];
        rightUpView.backgroundColor = @"#1D1D1D".color;
        [self addSubview:rightUpView];
        
        UIView* rightDownView = [UIKitFactory lineViewWithFrame:CGRectMake(rightTimeLab.zy_x - 23, rightUpView.zy_bottom + 40, 20.5, 1)];
        rightDownView.backgroundColor = @"#1D1D1D".color;
        [self addSubview:rightDownView];
        
        UIButton* okBtn = [UIKitFactory buttonWithTitle:@"确   定" fontSize:13 target:self action:@selector(okBtnClick)];
        okBtn.frame = CGRectMake(0, self.pickerView.zy_bottom, SCREEN_WIDTH, 41.5);
        [okBtn setBackgroundColor:@"#1373FF".color];
        [self addSubview:okBtn];
        
    }
    
    return self;
}

- (void)setShowType:(PickerTimeViewType)showType{
    
    switch (showType) {
        case PickerTimeViewTypeOrderTime:
            self.rightTimeLab.hidden = NO;
            self.leftTimeLab.hidden = NO;
            break;
            
        case PickerTimeViewTypeOther:
            self.rightTimeLab.hidden = YES;
            self.leftTimeLab.hidden = YES;
            break;
        default:
            break;
    }
}


- (void)cancelBtnClick{
    
    [[TLTransition  topController] dismissViewControllerAnimated:YES completion:nil];
}

- (void)okBtnClick{
    
    [self cancelBtnClick];
    
    if (self.blockDidSelect) {
    
        self.blockDidSelect(self.firstSelItem, self.secondSelItem);
    }
}

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 2;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    if(component ==0){
        return self.firstDataSource.count;
    }else{
        return self.secondDataSource.count;
    }
}

#pragma mark - UIPickerViewDelegate
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
    //设置分割线的颜色
    for(UIView *singleLine in pickerView.subviews)
    {
        if (singleLine.frame.size.height < 1)
        {
            singleLine.backgroundColor = AppLineColor;
        }
        
        singleLine.backgroundColor = [UIColor clearColor];
       
    }
  
    UILabel *labPicker = (UILabel *)view;
    
    if (labPicker == nil) {
        labPicker = [[UILabel alloc] init];
        labPicker.font = [UIFont systemFontOfSize:18.0f];
        labPicker.textColor = UIColor.blackColor;
        labPicker.textAlignment = NSTextAlignmentCenter;
       
        labPicker.adjustsFontSizeToFitWidth = YES;
    }
    
    labPicker.text = [self pickerView:pickerView titleForRow:row forComponent:component];
    
    if (component ==1) {
        labPicker.textAlignment = NSTextAlignmentLeft;
        labPicker.text = [NSString  stringWithFormat:@"         %@",[self pickerView:pickerView titleForRow:row forComponent:component]];
    }
    
   
    return labPicker;
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    if(component ==0){
        return self.firstDataSource[row];
    }else{
        return self.secondDataSource[row];
    }
}

- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)componen
{
    return 44.0f;
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    if (component ==0) {
        self.firstSelItem = [NSString  stringWithFormat:@"%@",self.firstDataSource[row]];
    }else{
        self.secondSelItem = [NSString  stringWithFormat:@"%@",self.secondDataSource[row]];
    }
}

@end

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值