OC 版 定制化 PickerView

//
//  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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: h5pickerview是一个基于H5技术的选择器组件。它可以在移动端的网页中使用,实现选择日期、时间、省市区等功能。 h5pickerview的特点有以下几个方面: 1. 轻量级:h5pickerview是一个轻量级的插件,它的体积小,加载速度快,不会占用太多的资源。 2. 灵活可定制:h5pickerview提供了丰富的配置项,可以根据需求自定义选择器的样式和行为。可以选择日期范围、设定初始值等。 3. 跨平台兼容:由于基于H5技术开发,h5pickerview可以兼容多个平台,无论是iOS、Android还是其他系统,都能够正常运行。 4. 使用简单:h5pickerview的使用非常简单,只需要在页面中引入相关的样式文件和脚本文件,然后在需要的地方添加相应的HTML标签即可。 5. 功能丰富:h5pickerview支持选择日期、时间、省市区等功能,用户可以方便地进行选择操作,满足不同场景下的需求。 总结来说,h5pickerview是一个方便实用的选择器组件,它能够帮助开发者快速实现选择日期、时间等功能,在移动端网页中提供更好的用户体验。无论是在线预约、订单提交还是其他需要选择操作的场景,h5pickerview都能够发挥重要作用。 ### 回答2: h5pickerview 是一种基于H5开发的日期和时间选择器。这个选择器可以在移动设备的浏览器中使用,通过H5代码进行调用和使用。 h5pickerview 可以通过HTML5的input标签的type属性来实现,比如可以使用type="date"实现日期的选择,使用type="time"实现时间的选择。此外,还可以通过JavaScript进行更加定制的使用。 h5pickerview 具有简单易用的特点,开发者可以根据自己的需求来配置和使用。通过简单的HTML代码和JavaScript代码,可以轻松地调用和集成到页面中。 h5pickerview 支持多种配置选项,比如可以设置日期的起始和结束范围,可以设置日期和时间的格式等等。这样,可以根据具体的需求来自定义选择器的外观和行为。 总的来说,h5pickerview 是一种方便快捷的日期和时间选择器,通过H5开发可以在移动设备的浏览器中使用。它具有简单易用、灵活可配置等优点,能够满足开发者的各种需求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值