本文可支持单滚轮选择器和双滚轮联动选择器
#pragma 调用方法
1. 单滚轮选择器的调用方法
sourceList为选择器中显示的数据,格式为列表形式:@[@"小时代",@"爵迹",@"幻城", @"更多"];
pickerViewStyle为选择器的模式,分单滚轮和双滚轮联动:RMPickerViewStyleSingle,RMPickerViewStyleDouble此处选择RMPickerViewStyleSingle;
show:选择器弹起动画
remove:选择器移除动画
遵循协议方法;
- (void)pickerView:(RMPickerView *)pickerView ButtonWithTag:(RMPickerViewButtonTag)buttonTag{
switch (buttonTag) {
case RMPickerViewButtonTagOfCancle: {
[pickerView remove];
break;
}
case RMPickerViewButtonTagOfConfirm: {
RMLog(@"sure");
RMLog(@"%d",pickerView.firstListIndex);
RMLog(@"%@",_pickerView.sourceLists[pickerView.firstListIndex]);
[pickerView remove];
break;
}
}
}
2. 双滚轮联动选择器调用方法
sourceList为选择器中显示的数据,格式为列表形式:
@[@{@"date":@"2016/06/24",@"time_list":@[@"12:00 - 18:00",@"19:00-24:00"]},
@{@"date":@"2016-06-24",@"time_list":@[@"9:00 - 11:00",@"12:00 - 18:00",@"19:00-24:00"]},
@{@"date":@"郭敬明",@"time_list":@[@"9:00 - 11:00",@"12:00 - 18:00",@"19:00-24:00"]}
];
pickerViewStyle为选择器的模式,分单滚轮和双滚轮联动:RMPickerViewStyleSingle,RMPickerViewStyleDouble 此处选择RMPickerViewStyleDouble;
show:选择器弹起动画
remove:选择器移除动画
遵循协议方法;
- (void)pickerView:(RMPickerView *)pickerView ButtonWithTag:(RMPickerViewButtonTag)buttonTag{
switch (buttonTag) {
case RMPickerViewButtonTagOfCancle: {
RMLog(@"cancle");
[pickerView remove];
break;
}
case RMPickerViewButtonTagOfConfirm: {
RMLog(@"sure");
RMLog(@"%d,%d",pickerView.firstListIndex, pickerView.secondListIndex);
RMLog(@"%@,%@",_pickerView.sourceLists[pickerView.firstListIndex][@"date"],_pickerView.sourceLists[pickerView.firstListIndex][@"time_list"][pickerView.secondListIndex] );
[pickerView remove];
break;
}
}
}
此外创建RMPickerView最好使用属性管理。数据格式可以自行定义修改
//
// RMPickerView.h
// SkyGoods
//
// Created by 王林 on 16/6/27.
// Copyright © 2016年 com.benba. All rights reserved.
//
#import
@class RMPickerView;
//自定义选择器类型,方便以后扩展,不算个数
typedef NS_ENUM(NSUInteger, RMPickerViewStyle) {
RMPickerViewStyleDefault = 0,//默认单滚轮
RMPickerViewStyleSingle,//单滚轮
RMPickerViewStyleDouble,//双滚轮联动
};
typedef NS_ENUM(NSUInteger, RMPickerViewButtonTag) {
RMPickerViewButtonTagOfCancle = 101,//取消
RMPickerViewButtonTagOfConfirm//确定
};
@protocol RMPickerViewDelegate <NSObject>
- (void) pickerView:(RMPickerView *)pickerView ButtonWithTag:(RMPickerViewButtonTag)buttonTag;
@end
@interface RMPickerView : UIView
@property (nonatomic)RMPickerViewStyle pickerViewStyle;
@property (copy,nonatomic)NSArray *sourceLists;
@property (weak,nonatomic)id<RMPickerViewDelegate>delegate;
@property (nonatomic)NSInteger firstListIndex;
@property (nonatomic)NSInteger secondListIndex;
- (void) show;
- (void) remove;
@end
//
// RMPickerView.m
// SkyGoods
//
// Created by 王林 on 16/6/27.
// Copyright © 2016年 com.benba. All rights reserved.
//
#import "RMPickerView.h"
const CGFloat kPickerViewHeight =216.f;
const CGFloat kPickerViewToolBarHeight =40.f;
const CGFloat kPickerViewDuringTime =0.5f;
const CGFloat kPickerViewRowHeight =40.f;
const CGFloat kPickerViewButtonWidth =60.f;
//滚轮的个数
typedef NS_ENUM(NSUInteger, RMPickerViewComponentCount) {
RMPickerViewComponentCountOfSingle = 1,
RMPickerViewComponentCountOfDouble
};
@interface RMPickerView()<UIPickerViewDelegate,UIPickerViewDataSource>
{
UIPickerView *_pickerView;
UIView *_pickerBackView;
RMPickerViewComponentCount _componentCount;
}
@end
@implementation RMPickerView
- (instancetype)initWithFrame:(CGRect)frame{
self = [superinitWithFrame:frame];
if (self) {
self.backgroundColor = [UIColorcolorWithWhite:0.902alpha:0.2];
[selfcreateUI];
}
returnself;
}
- (void) createUI{
_pickerBackView = [[UIViewalloc]initWithFrame:CGRectMake(0,kScreenHeight,kScreenWidth,kPickerViewHeight + kPickerViewToolBarHeight)];
_pickerBackView.backgroundColor = [UIColorwhiteColor];
[selfaddSubview:_pickerBackView];
[selfaddToolBar];
[selfcreatePickerView];
UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizeralloc]initWithTarget:selfaction:@selector(tapGesture:)];
[selfaddGestureRecognizer:tapGestureRecognizer];
}
- (void) addToolBar{
UIView *toolBar = [[UIViewalloc]initWithFrame:CGRectMake(0,0,kScreenWidth,kPickerViewToolBarHeight)];
toolBar.backgroundColor = [UIColorgroupTableViewBackgroundColor];
[_pickerBackViewaddSubview:toolBar];
// 取消
UIButton *cancelButton = [RMUtilsbuttonWith:CGRectMake(0,0,kPickerViewButtonWidth,kPickerViewToolBarHeight)text:@"取消"backColor:[UIColorclearColor]textColor:[RMUtilsthemeColor]tag:RMPickerViewButtonTagOfCancle];
cancelButton.titleLabel.font = [UIFontsystemFontOfSize:15];
[cancelButton addTarget:selfaction:@selector(buttonAction:)forControlEvents:UIControlEventTouchUpInside];
[toolBar addSubview:cancelButton];
// 确定
UIButton *confirmButton = [RMUtilsbuttonWith:CGRectMake(kScreenWidth -kPickerViewButtonWidth,0,kPickerViewButtonWidth,kPickerViewToolBarHeight)text:@"确定"backColor:[UIColorclearColor]textColor:[RMUtilsthemeColor]tag:RMPickerViewButtonTagOfConfirm];
confirmButton.titleLabel.font = [UIFontsystemFontOfSize:15];
[confirmButton addTarget:selfaction:@selector(buttonAction:)forControlEvents:UIControlEventTouchUpInside];
[toolBar addSubview:confirmButton];
}
- (void) createPickerView{
_pickerView = [[UIPickerViewalloc]initWithFrame:CGRectMake(0,kPickerViewToolBarHeight,kScreenWidth,kPickerViewHeight)];
_pickerView.delegate =self;
_pickerView.dataSource =self;
[_pickerViewsetShowsSelectionIndicator:YES];
[_pickerBackViewaddSubview:_pickerView];
}
- (void) buttonAction:(UIButton *)button{
if (self.delegate && [self.delegaterespondsToSelector:@selector(pickerView:ButtonWithTag:)]) {
[self.delegatepickerView:selfButtonWithTag:button.tag];
}
}
- (void)tapGesture:(UIGestureRecognizer *)tap{
[selfremove];
}
- (void) show{
[UIViewanimateWithDuration:kPickerViewDuringTimeanimations:^{
[_pickerBackViewsetY:kScreenHeight -kPickerViewToolBarHeight -kPickerViewHeight];
}];
}
- (void) remove{
[UIViewanimateWithDuration:kPickerViewDuringTimeanimations:^{
[_pickerBackViewsetY:kScreenHeight];
} completion:^(BOOL finished) {
[selfremoveFromSuperview];
}];
}
- (void)setPickerViewStyle:(RMPickerViewStyle)pickerViewStyle{
switch (pickerViewStyle) {
caseRMPickerViewStyleDefault: {
_componentCount =RMPickerViewComponentCountOfSingle;
break;
}
caseRMPickerViewStyleSingle: {
_componentCount =RMPickerViewComponentCountOfSingle;
break;
}
caseRMPickerViewStyleDouble: {
_componentCount =RMPickerViewComponentCountOfDouble;
break;
}
default:{
_componentCount =RMPickerViewComponentCountOfSingle;
break;
}
}
}
#pragma pickerView delegate && dateSource
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
return_componentCount;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
if (component ==0) {
return [self.sourceListscount];
}else{
NSArray *secondList =self.sourceLists[_firstListIndex][@"time_list"];
return [secondListcount];
}
}
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component{
returnkScreenWidth/_componentCount;
}
- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component{
returnkPickerViewRowHeight;
}
/*
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
NSString *title;
if (_componentNumber == 1) {
title = self.sourceLists[row];
}else if (_componentNumber == 2){
if (component == 0) {
title = self.sourceLists[row][@"date"];
}else{
title = self.sourceLists[_firstListIndex][@"time_list"][row];
}
}
return title;
}*/
//原生选择器的字体太大,可以选择自定义
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
if (!view) {
view = [[UIViewalloc]initWithFrame:CGRectMake(0,0,kScreenWidth/_componentCount,kPickerViewRowHeight)];
NSString *title;
if (_componentCount ==RMPickerViewComponentCountOfSingle) {
title = self.sourceLists[row];
}elseif (_componentCount ==RMPickerViewComponentCountOfDouble){
if (component ==0) {
title = self.sourceLists[row][@"date"];
}else{
title = self.sourceLists[_firstListIndex][@"time_list"][row];
}
}
UILabel *rowLabel = [RMUtilslabelWith:view.boundsfont:[UIFontboldSystemFontOfSize:16]text:titletextColor:[UIColorblackColor]];
rowLabel.textAlignment =NSTextAlignmentCenter;
[view addSubview:rowLabel];
}
return view;
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
if (_componentCount ==RMPickerViewComponentCountOfSingle) {
_firstListIndex = row;
}elseif (_componentCount ==RMPickerViewComponentCountOfDouble){
if (component ==0) {
_firstListIndex = row;
_secondListIndex =0;
[_pickerViewreloadComponent:1];
[_pickerViewselectRow:0inComponent:1animated:YES];
}else{
_secondListIndex = row;
}
}
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
// Drawing code
}
*/
@end
测试图如图所示: