1.UIPickView(适用于本地添加数据)
项目中经常会需要自定义pickView;如下效果图
点击接送范围后弹出自定义的pickView
封装的代码如下:
(底部放的是一张View)
图片所在控制器中调用的代码:
2.UIDatePickView
选择出发时间弹出自定义的UIDatePickView(底部放一张view)
3.支持“网络+本地”数据选择 CDZPicker
效果图
封装的.h文件
#import
//NS_ASSUME_NONNULL_BEGIN
typedef void (^CDZCancelBlock)(void);
typedef void (^CDZConfirmBlock)(NSArray *strings, NSArray *indexs);
@interface CDZPickerComponentObject : NSObject
@property (nonatomic, strong, nullable) NSMutableArray *subArray;
@property (nonatomic, copy) NSString *text;
- (instancetype)initWithText:(NSString *)text subArray:(NSMutableArray *)array;
- (instancetype)initWithText:(NSString *)text;
@end
@interface CDZPickerBuilder : NSObject
//是否显示背后遮罩,默认为YES
@property (nonatomic, assign, getter=isShowMask) BOOL showMask;
//确认按钮的文字,默认为“确认”
@property (nonatomic, copy, nullable) NSString *confirmText;
//取消按钮的文字,默认为“取消”
@property (nonatomic, copy, nullable) NSString *cancelText;
//确认文字的颜色,默认是蓝色
@property (nonatomic, strong, nullable) UIColor *confirmTextColor;
//取消文字的颜色,默认为蓝色
@property (nonatomic, strong, nullable) UIColor *cancelTextColor;
//选择器的背景颜色,默认为白色
@property (nonatomic, strong, nullable) UIColor *pickerColor;
//选择器的文字颜色,默认为黑色
@property (nonatomic, strong, nullable) UIColor *pickerTextColor;
//默认滚动的行数,默认为第1行
@property (nonatomic, assign) NSInteger defaultIndex;
//整个pickerView的高度,默认为248,包括44的按钮栏
@property (nonatomic, assign) CGFloat pickerHeight;
@end
@interface CDZPicker : UIView
/**
单行数据
@param view 所在view
@param builder 配置
@param strings 单个string数组
@param confirmBlock 点击确认后
@param cancelBlcok 点击取消后
*/
+ (void)showSinglePickerInView:(UIView *)view
withBuilder:(nullable CDZPickerBuilder *)builder
strings:(NSArray *)strings
confirm:(CDZConfirmBlock)confirmBlock
cancel:(CDZCancelBlock)cancelBlcok;
/**
多行不联动数据
@param view 所在view
@param builder 配置
@param arrays 二维数组,数组里string数组个数为行数,互相独立
@param confirmBlock 点击确认后
@param cancelBlcok 点击取消后
*/
+ (void)showMultiPickerInView:(UIView *)view
withBuilder:(nullable CDZPickerBuilder *)builder
stringArrays:(NSArray *> *)arrays
confirm:(CDZConfirmBlock)confirmBlock
cancel:(CDZCancelBlock)cancelBlcok;
/**
多行联动数据
@param view 所在的view
@param builder 配置
@param components 传入componetobject数组,可嵌套
@param confirmBlock 点击确认后
@param cancelBlcok 点击取消后
*/
+ (void)showLinkagePickerInView:(UIView *)view
withBuilder:(nullable CDZPickerBuilder *)builder
components:(NSArray *)components
confirm:(CDZConfirmBlock)confirmBlock
cancel:(CDZCancelBlock)cancelBlcok;
@end
//NS_ASSUME_NONNULL_END
封装的.m文件
#import "CDZPicker.h"
#define SCREEN_WIDTH ([UIScreen mainScreen].bounds.size.width)
#define SCREEN_HEIGHT ([UIScreen mainScreen].bounds.size.height)
#define BACKGROUND_BLACK_COLOR [UIColor colorWithRed:0.412 green:0.412 blue:0.412 alpha:0.7]
static const NSInteger CDZPickerViewDefaultHeight = 248;
static const NSInteger CDZToolBarHeight = 44;
@implementation CDZPickerComponentObject
- (instancetype)init{
return [self initWithText:@"" subArray:[NSMutableArray array]];
}
- (instancetype)initWithText:(NSString *)text{
return [self initWithText:text subArray:[NSMutableArray array]];
}
- (instancetype)initWithText:(NSString *)text subArray:(NSMutableArray *)array{
if (self = [super init]) {
_text = text;
_subArray = array;
}
return self;
}
@end
@implementation CDZPickerBuilder
- (instancetype)init{
if (self = [super init]) {
_showMask = YES;
_defaultIndex = 0;
_pickerHeight = CDZPickerViewDefaultHeight;
}
return self;
}
@end
@interface CDZPicker()
@property (nonatomic, assign, getter=isLinkage) BOOL linkage;
@property (nonatomic, assign) NSInteger numberOfComponents;
@property (nonatomic, strong) CDZPickerBuilder *builder;
@property (nonatomic, copy) NSArray *componets;
@property (nonatomic, copy) CDZConfirmBlock confirmBlock;
@property (nonatomic, copy) CDZCancelBlock cancelBlock;
@property (nonatomic, copy) NSArray *> *stringArrays;
@property (nonatomic, strong) NSMutableArray *> *rows;
@property (nonatomic, strong) UIPickerView *pickerView;
@property (nonatomic, strong) UIButton *confirmButton;
@property (nonatomic, strong) UIButton *cancelButton;
@property (nonatomic, strong) UIView *containerView;
@end
@implementation CDZPicker
#pragma mark - setup
- (void)config{
if (!self.isLinkage) {
self.numberOfComponents = self.stringArrays.count;
}
else{
self.rows = [NSMutableArray array];
CDZPickerComponentObject *object = self.componets.firstObject;
[self.rows setObject:[NSMutableArray arrayWithArray:self.componets] atIndexedSubscript:0];
for (self.numberOfComponents = 1;; self.numberOfComponents++) {
[self.rows setObject:object.subArray atIndexedSubscript:self.numberOfComponents];
object = [self objectAtIndex:0 inObject:object];
if (!object) {
break;
}
}
}
[self setupViews];
}
+ (void)showSinglePickerInView:(UIView *)view
withBuilder:(CDZPickerBuilder *)builder
strings:(NSArray *)strings
confirm:(CDZConfirmBlock)confirmBlock
cancel:(CDZCancelBlock)cancelBlcok{
CDZPicker *pickerView = [[CDZPicker alloc]initWithFrame:view.frame];
NSMutableArray *tmp = [NSMutableArray arrayWithCapacity:strings.count];
for (NSString *string in strings) {
CDZPickerComponentObject *object = [[CDZPickerComponentObject alloc]initWithText:string];
[tmp addObject:object];
}
pickerView.linkage = YES;
pickerView.componets = [tmp copy];
pickerView.confirmBlock = confirmBlock;
pickerView.cancelBlock = cancelBlcok;
pickerView.builder = builder ?:[CDZPickerBuilder new];
[pickerView config];
[view addSubview:pickerView];
}
+ (void)showMultiPickerInView:(UIView *)view
withBuilder:(CDZPickerBuilder *)builder
stringArrays:(NSArray *> *)arrays
confirm:(CDZConfirmBlock)confirmBlock cancel:(CDZCancelBlock)cancelBlcok{
CDZPicker *pickerView = [[CDZPicker alloc]initWithFrame:view.frame];
pickerView.linkage = NO;
pickerView.stringArrays = arrays;
pickerView.confirmBlock = confirmBlock;
pickerView.cancelBlock = cancelBlcok;
pickerView.builder = builder ?:[CDZPickerBuilder new];
[pickerView config];
[view addSubview:pickerView];
}
+ (void)showLinkagePickerInView:(UIView *)view
withBuilder:(CDZPickerBuilder *)builder
components:(NSArray *)components
confirm:(CDZConfirmBlock)confirmBlock
cancel:(CDZCancelBlock)cancelBlcok{
CDZPicker *pickerView = [[CDZPicker alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
pickerView.linkage = YES;
pickerView.componets = components;
pickerView.confirmBlock = confirmBlock;
pickerView.cancelBlock = cancelBlcok;
pickerView.builder = builder ?:[CDZPickerBuilder new];
[pickerView config];
[view addSubview:pickerView];
}
- (void)setupViews{
self.backgroundColor = self.builder.isShowMask ? BACKGROUND_BLACK_COLOR : UIColor.clearColor;
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(dissView)];
[self addGestureRecognizer:tap];
[self addSubview:self.containerView];
[self.containerView addSubview:self.pickerView];
[self.containerView addSubview:self.confirmButton];
[self.containerView addSubview:self.cancelButton];
NSInteger defaultIndex = (self.builder.defaultIndex < self.numberOfComponents && self.builder.defaultIndex > 0) ? self.builder.defaultIndex : 0;
[self.pickerView selectRow:defaultIndex inComponent:0 animated:NO];
}
#pragma mark - event response
- (void)confirm:(UIButton *)button{
if (self.confirmBlock){
NSMutableArray *resultStrings = [NSMutableArray arrayWithCapacity:self.numberOfComponents];
NSMutableArray *resultIndexs = [NSMutableArray arrayWithCapacity:self.numberOfComponents];
if ([self configResultStrings:resultStrings indexs:resultIndexs]) {
self.confirmBlock([resultStrings copy],[resultIndexs copy]);
}
}
[self removeFromSuperview];
}
- (void)dissView{
if (self.cancelBlock) {
self.cancelBlock();
}
[self removeFromSuperview];
}
- (void)cancel:(UIButton *)button{
[self dissView];
}
#pragma mark - private
- (BOOL)configResultStrings:(NSMutableArray *)strings indexs:(NSMutableArray *)indexs{
if (!strings || !indexs) {
return NO;
}
[strings removeAllObjects];
[indexs removeAllObjects];
if (!self.isLinkage) {
for (NSInteger index = 0; index < self.numberOfComponents; index++) {
NSInteger indexRow = [self.pickerView selectedRowInComponent:index];
[indexs addObject:@(indexRow)];
NSArray *tmp = self.stringArrays[index];
if (tmp.count > 0) {
[strings addObject:tmp[indexRow]];
}
}
}
else{
for (NSInteger index = 0; index < self.numberOfComponents; index++) {
NSInteger indexRow = [self.pickerView selectedRowInComponent:index];
[indexs addObject:@(indexRow)];
NSMutableArray *tmp = self.rows[index];
if (tmp.count > 0) {
[strings addObject:tmp[indexRow].text];
}
}
}
return YES;
}
- (CDZPickerComponentObject *)objectAtIndex:(NSInteger)index inObject:(CDZPickerComponentObject *)object{
if (object.subArray.count > index) {
return object.subArray[index];
}
return nil;
}
#pragma mark - PickerDataSource
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
return self.numberOfComponents;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
if (!self.isLinkage) {
return self.stringArrays[component].count;
}
else{
return self.rows[component].count;
}
}
#pragma mark - PickerDelegate
- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component{
return 44;
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
if (!self.isLinkage) {
return;
}
if (component < (self.numberOfComponents - 1)) {
NSMutableArray *tmp = self.rows[component];
if (tmp.count > 0) {
tmp = tmp[row].subArray;
}
[self.rows setObject:((tmp.count > 0) ? tmp : [NSMutableArray array]) atIndexedSubscript:component + 1];
[self pickerView:pickerView didSelectRow:0 inComponent:component + 1];
[pickerView selectRow:0 inComponent:component + 1 animated:NO];
}
[pickerView reloadComponent:component];
}
- (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 = UIColor.clearColor;
}
}
//设置文字的属性
UILabel *genderLabel = [UILabel new];
genderLabel.textAlignment = NSTextAlignmentCenter;
genderLabel.font = [UIFont systemFontOfSize:23.0];
genderLabel.textColor = self.builder.pickerTextColor ?: UIColor.blackColor;
if (!self.isLinkage) {
NSArray *tmp = self.stringArrays[component];
if (tmp.count > 0) {
genderLabel.text = tmp[row];
}
}
else{
NSArray *tmp = self.rows[component];
if (tmp.count > 0) {
genderLabel.text = tmp[row].text;
}
}
return genderLabel;
}
#pragma mark - getter
- (UIView *)containerView{
if (!_containerView) {
CGFloat pickerViewHeight = self.builder.pickerHeight > CDZToolBarHeight ? self.builder.pickerHeight : CDZPickerViewDefaultHeight;
_containerView = [[UIView alloc]initWithFrame:CGRectMake(0, SCREEN_HEIGHT - pickerViewHeight, SCREEN_WIDTH, pickerViewHeight)];
_containerView.backgroundColor = self.builder.pickerColor ?: UIColor.whiteColor;
}
return _containerView;
}
- (UIButton *)confirmButton{
if (!_confirmButton) {
_confirmButton = [[UIButton alloc] initWithFrame:CGRectMake(SCREEN_WIDTH -70, 10, 40, 30)];
_confirmButton.backgroundColor = UIColor.clearColor;
_confirmButton.titleLabel.font = [UIFont systemFontOfSize:18.0];
NSString *title = self.builder.confirmText.length ? self.builder.confirmText : @"确定";
[_confirmButton setTitle:title forState:UIControlStateNormal];
UIColor *color = self.builder.confirmTextColor ?: UToColor.redColor;
[_confirmButton setTitleColor:color forState:UIControlStateNormal];
[_confirmButton addTarget:self action:@selector(confirm:) forControlEvents:UIControlEventTouchUpInside];
}
return _confirmButton;
}
- (UIButton *)cancelButton{
if (!_cancelButton) {
_cancelButton = [[UIButton alloc] initWithFrame:CGRectMake(30, 10, 40, 30)];
_cancelButton.backgroundColor = UIColor.clearColor;
_cancelButton.titleLabel.font = [UIFont systemFontOfSize:18.0];
NSString *title = self.builder.cancelText.length ? self.builder.cancelText : @"取消";
[_cancelButton setTitle:title forState:UIControlStateNormal];
UIColor *color = self.builder.cancelTextColor ?: UToColor.redColor;
[_cancelButton setTitleColor:color forState:UIControlStateNormal];
[_cancelButton addTarget:self action:@selector(cancel:) forControlEvents:UIControlEventTouchUpInside];
}
return _cancelButton;
}
- (UIPickerView *)pickerView{
if (!_pickerView) {
CGFloat pickerViewHeight = self.builder.pickerHeight > CDZToolBarHeight ? self.builder.pickerHeight : CDZPickerViewDefaultHeight;
_pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0,CDZToolBarHeight, SCREEN_WIDTH, pickerViewHeight - CDZToolBarHeight)];
_pickerView.backgroundColor = UIColor.clearColor;
_pickerView.delegate = self;
_pickerView.dataSource = self;
}
return _pickerView;
}
@end
调用地方示例: