选择器,我想大家都不陌生,当需要用户去选择某些范围值内的一个固定值时,我们会采用选择器的方式。选择器可以直观的提示用户选择的值范围、统一信息的填写格式,同时也方便用户快速的进行选择,比如对于性别,正常情况下就只有男女两种情况,那这时候用一个选择器给用户进行选择的话,可以避免错误数据的输入,也更方便用户去填写。再比如需要获取用户的生日信息时,采用选择器的方式可以统一生日的格式,如果让用户自行输入的话,可能会出现各种各样的生日信息格式,不利于数据的存储,但是采用选择器的方式的话,用户可找到对应的日期进行选择即可。
在iOS有专门的一个选择器类UIPickerView
,进入UIPickerView的头文件,我们可以发现 UIPickerView直接继承了UIView,其事件处理通过代理方法完成,所以创建UIPickerView的时候记得签它的代理UIPickerViewDataSource
和UIPickerViewDelegate
。其实它和UITbleView有点像,不过UIPickerView还多了个列属性,UITbleView我们都很熟了,所以可以对比UITbleView的相关属性和代理方法来学习记忆UIPickerView。
一. UIPickerView的代理方法
(1)UIPickerViewDataSource对应的代理方法有(其代理方法必须要实现):
返回显示的列数
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView;
返回每一列中需要显示的行数
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component;
(2)UIPickerViewDelegate对应的代理方法(其代理方法根据需求进行选择性实现):
返回每一列的宽度
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component;
返回每一列的高度
- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component ;
返回UIPickerView控件中指定列的列表项的要显示的内容
- (nullable NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component ;
返回UIView,作为该UIPickerView控件中指定列的指定行的显示视图
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(nullable UIView *)view ;
选择指定列中的指定行
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component ;
二、UIPickerView和UIDatePicker。
UIDatePicker是系统帮我们封装好的一个时间日期选择器,继承于UIControl,UIDatePicker有一定的局限性,因为它只有四种显示样式:
UIDatePickerModeTime,
UIDatePickerModeDate,
UIDatePickerModeDateAndTime,
UIDatePickerModeCountDownTimer,
如果需求和这四种样式都不符合的话,那么就没办法使用UIDatePicker,比如当只需要显示年、月信息的时候,显然UIDatePicker没办法满足我们的需求,那这时我们只能通过UIPickerView来自定义自己想要的选择器。
三、UIPickerView的自定义使用
(1)创建基类继承于UIView的WXZBasePickView。
我们常见的选择器的样式是一个带透明背景色的view,底部是内容的选择器,有确定和取消按钮,大致如图:
所以我们创建一个基类view,这个view的样式如图所示样式,之后根据内容的差别创建基于该view的选择器。
在.h中声明各个属性及方法
#import <UIKit/UIKit.h>
@interface WXZBasePickView : UIView
@property (nonatomic, strong) UIView *contentView;
//选择器
@property (nonatomic, strong)UIPickerView *pickerView;
//取消按钮
@property (nonatomic, strong)UIButton *cancelButton;
//确定按钮
@property (nonatomic, strong)UIButton *confirmButton;
//选择器每一列的高度
@property (nonatomic, assign)CGFloat pickerViewHeight;
/**
* 创建视图,初始化视图时初始数据
*/
- (void)initPickView;
/**
* 确认按钮的点击事件
*/
- (void)clickConfirmButton;
/**
* pickerView的显示
*/
- (void)show;
/**
* 移除pickerView
*/
- (void)disMiss;
@end
在.m中实现相关方法
#import "WXZBasePickView.h"
#define ScreenWidth [UIScreen mainScreen].bounds.size.width
#define ScreenHeight [UIScreen mainScreen].bounds.size.height
@implementation WXZBasePickView
- (instancetype)init
{
self = [super init];
if (self) {
_pickerViewHeight = 250;
self.bounds = [UIScreen mainScreen].bounds;
self.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.64];
self.layer.opacity = 0.0;
UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(disMiss)];
self.userInteractionEnabled = YES;
[self addGestureRecognizer:tap];
[self addSubview:self.contentView];
[self.contentView addSubview:self.pickerView];
[self.contentView addSubview:self.cancelButton];
[self.contentView addSubview:self.confirmButton];
[self initPickView];
}
return self;
}
//初始化选择器内容,创建子类时需实现该父类方法
-(void)initPickView{
}
//点击确定按钮
- (void)clickConfirmButton
{
[self disMiss];
}
//点击取消按钮
- (void) clickCancelButton
{
[self disMiss];
}
//显示选择器
- (void)show
{
[[UIApplication sharedApplication].keyWindow addSubview:self];
[self setCenter:[UIApplication sharedApplication].keyWindow.center];
[[UIApplication sharedApplication].keyWindow bringSubviewToFront:self];
CGRect frame = self.contentView.frame;
frame.origin.y -= self.contentView.frame.size.height;
[UIView animateWithDuration:0.4 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
[self.layer setOpacity:1.0];
self.contentView.frame = frame;
} completion:^(BOOL finished) {
}];
}
//移除选择器
- (void)disMiss
{
CGRect frame = self.contentView.frame;
frame.origin.y += self.contentView.frame.size.height;
[UIView animateWithDuration:0.4 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
[self.layer setOpacity:0.0];
self.contentView.frame = frame;
} completion:^(BOOL finished) {
[self removeFromSuperview];
}];
}
//设置选择器的高度
- (void)setPickerViewHeight:(CGFloat)pickerViewHeight
{
_pickerViewHeight = pickerViewHeight;
self.contentView.frame = CGRectMake(self.contentView.frame.origin.x, self.contentView.frame.origin.y, self.contentView.frame.size.width, pickerViewHeight);
}
- (UIView *)contentView
{
if (!_contentView) {
_contentView = [[UIView alloc]initWithFrame:CGRectMake(0, ScreenHeight, ScreenWidth, self.pickerViewHeight)];
[_contentView setBackgroundColor:[UIColor whiteColor]];
}
return _contentView;
}
- (UIPickerView *)pickerView
{
if (!_pickerView) {
_pickerView = [[UIPickerView alloc]initWithFrame:CGRectMake(0, 0, self.contentView.frame.size.width, self.contentView.frame.size.height)];
[_pickerView setBackgroundColor:[UIColor whiteColor]];
}
return _pickerView;
}
- (UIButton *)cancelButton
{
if (!_cancelButton) {
_cancelButton = [[UIButton alloc]initWithFrame:CGRectMake(16, 0, 44, 44)];
[_cancelButton setTitle:@"取消" forState:UIControlStateNormal];
[_cancelButton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
[_cancelButton.titleLabel setFont:[UIFont systemFontOfSize:16]];
[_cancelButton addTarget:self action:@selector(clickCancelButton) forControlEvents:UIControlEventTouchUpInside];
}
return _cancelButton;
}
- (UIButton *)confirmButton
{
if (!_confirmButton) {
_confirmButton = [[UIButton alloc]initWithFrame:CGRectMake(self.contentView.frame.size.width - self.cancelButton.frame.size.width - self.cancelButton.frame.origin.x, self.cancelButton.frame.origin.y, self.cancelButton.frame.size.width, self.cancelButton.frame.size.height)];
[_confirmButton setTitle:@"确定" forState:UIControlStateNormal];
[_confirmButton setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
[_confirmButton.titleLabel setFont:[UIFont systemFontOfSize:16]];
[_confirmButton addTarget:self action:@selector(clickConfirmButton) forControlEvents:UIControlEventTouchUpInside];
}
return _confirmButton;
}
@end
(2)、自定义一个日期选择器,可选择显示年、月或者年、月、日以及是否显示“至今”选项。
创建继承于我们自定义好的WXZBasePickView的日期选择器WXZPickDateView
.h声明相关属性和方法
#import "WXZBasePickView.h"
@class WXZBasePickView;
//选择器的选择代理方法
@protocol PickerDateViewDelegate<NSObject>
- (void)pickerDateView:(WXZBasePickView *)pickerDateView selectYear:(NSInteger)year selectMonth:(NSInteger)month selectDay:(NSInt