基类选择器
.h文件
@interface BasePickerView : 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文件
@implementation BasePickerView
- (instancetype)init {
self = [super init];
if(self) {
_pickerViewHeight = 260+kBottomSafeHeight;
//设置此图层大小为主屏幕大小
self.bounds = [UIScreen mainScreen].bounds;
//设置背景为灰色
self.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.6];
//定义手势,在点击空白区域时,移除此图层
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];
//以便在子类中重写此方法,将pickerView所需的数据初始化
[self initPickView];
}
return self;
}
- (void)initPickView {
}
//初始化内容视图
- (UIView *)contentView
{
if (!_contentView) {
_contentView = [[UIView alloc]initWithFrame:CGRectMake(0, kScreenHeight, kScreenWidth, self.pickerViewHeight)];
[_contentView setBackgroundColor:[UIColor whiteColor]];
[_contentView bezierPathWithRoundingCorners:UIRectCornerTopLeft|UIRectCornerTopRight cornerRadii:CGSizeMake(10, 10)];
}
return _contentView;
}
//初始化选择器
- (UIPickerView *)pickerView
{
if (!_pickerView) {
_pickerView = [[UIPickerView alloc]initWithFrame:CGRectMake(0, 40, self.contentView.frame.size.width, self.contentView.frame.size.height-(kBottomSafeHeight+40))];
[_pickerView setBackgroundColor:[UIColor whiteColor]];
}
return _pickerView;
}
//初始化取消按钮
- (UIButton *)cancelButton {
if (!_cancelButton) {
_cancelButton = [[UIButton alloc]initWithFrame:CGRectMake(16, 10, 44, 50)];
[_cancelButton setTitle:@"取消" forState:UIControlStateNormal];
[_cancelButton setTitleColor:[UIColor colorWithHex:0x999999] forState:UIControlStateNormal];
[_cancelButton.titleLabel setFont:FONT_16];
[_cancelButton addTarget:self action:@selector(clickCancelButton) forControlEvents:UIControlEventTouchUpInside];
}
return _cancelButton;
}
//初始化确定按钮
- (UIButton *)confirmButton {
if (!_confirmButton) {
_confirmButton = [[UIButton alloc]initWithFrame:CGRectMake(self.contentView.width - self.cancelButton.width - self.cancelButton.x, self.cancelButton.y, self.cancelButton.width, self.cancelButton.height)];
[_confirmButton setTitle:@"确定" forState:UIControlStateNormal];
[_confirmButton setTitleColor:[UIColor colorWithHex:0x222222] forState:UIControlStateNormal];
[_confirmButton.titleLabel setFont:FONT_16];
[_confirmButton addTarget:self action:@selector(clickConfirmButton) forControlEvents:UIControlEventTouchUpInside];
}
return _confirmButton;
}
//等待继承他的子类重写此方法
- (void)clickConfirmButton {
[self disMiss];
}
- (void)clickCancelButton {
[self disMiss];
}
//获取keyWindow
-(UIWindow *)getKeyWindow{
id appDelegate = [UIApplication sharedApplication].delegate;
if (appDelegate && [appDelegate respondsToSelector:@selector(window)]) {
return [appDelegate window];
}
NSArray *windows = [UIApplication sharedApplication].windows;
if ([windows count] == 1) {
return [windows firstObject];
}
else {
for (UIWindow *window in windows) {
if (window.windowLevel == UIWindowLevelNormal) {
return window;
}
}
}
return nil;
}
//移除此图层
- (void)disMiss {
CGRect frameContent = self.contentView.frame;
// 相当于把contentView的y坐标设置为ScreenHeight,达到一个隐藏的目的
frameContent.origin.y += self.contentView.frame.size.height;
//设置动画效果
[UIView animateWithDuration:0.3 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
//将此图层不透明性设为0
[self.layer setOpacity:0];
// 再将内容视图隐藏
self.contentView.frame = frameContent;
} completion:^(BOOL finished) {
//将此图层从父视图中移除掉
[self removeFromSuperview];
}];
}
//推出此图层
- (void)show {
[[self getKeyWindow] addSubview:self];
[self setCenter:[self getKeyWindow].center];
[[self getKeyWindow] bringSubviewToFront:self];
CGRect frameContent = self.contentView.frame;
frameContent.origin.y -= self.contentView.frame.size.height;
[UIView animateWithDuration:0.3 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
[self.layer setOpacity:1.0];
self.contentView.frame = frameContent;
} completion:nil];
}
@end
城市选择器
.h
@protocol CityAreaPickerViewDelegate<NSObject> //定义一个代理。
//此方法目的是得到当前选择器的选择结果
- (void)pickerCityAreaView:(BasePickerView *)pickerDateView selectProvince:(NSString*)province selectCity:(NSString*)city selectArea:(NSString*)area;
@end
@interface CityAreaPickerView : BasePickerView<UIPickerViewDelegate,UIPickerViewDataSource>
@property (nonatomic, copy)NSString* selectProvince;
@property (nonatomic, copy)NSString* selectCity;
@property (nonatomic, copy)NSString* selectArea;
@property(nonatomic, weak)id <CityAreaPickerViewDelegate>delegate ;
@end
.m
@interface CityAreaPickerView ()
@property(nonatomic,strong)NSMutableArray *provinceArr;
@end
@implementation CityAreaPickerView
- (void)initPickView {
[super initPickView];
[self.pickerView setDelegate:self];
[self.pickerView setDataSource:self];
self.provinceArr = [[NSMutableArray alloc] init];
NSDictionary *dict = [self readLocalFileWithName:@"city"];
NSDictionary *data = [dict objectForKey:@"data"];
NSArray *areas = [data objectForKey:@"areas"];
for(NSDictionary *temp in areas){
CityAreaModel *model = [[CityAreaModel alloc] initWithDic:temp];
[self.provinceArr addObject:model];
}
}
// 读取本地JSON文件
- (NSDictionary *)readLocalFileWithName:(NSString *)name {
// 获取文件路径
NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:@"json"];
// 将文件数据化
NSData *data = [[NSData alloc] initWithContentsOfFile:path];
// 对数据进行JSON格式化并返回字典形式
return [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
}
//返回列数
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
return 3;
}
//每一行的高度
- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component {
return 40;
}
//返回行数
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
if (component == 0) {
return self.provinceArr.count;
} else if(component == 1) {
NSInteger selRow = [pickerView selectedRowInComponent:0];
CityAreaModel *model = self.provinceArr[selRow];
return [model.children count];
} else {
NSInteger selRow = [pickerView selectedRowInComponent:0];
CityAreaModel *model = self.provinceArr[selRow];
NSInteger sel1Row = [pickerView selectedRowInComponent:1];
CityModel *cityModel = model.children[sel1Row];
return [cityModel.children count];
}
return 0;
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
switch (component) {
case 0:
[pickerView reloadComponent:1];
[pickerView reloadComponent:2];
break;
case 1:
[pickerView reloadComponent:2];
default:
break;
}
}
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(nullable UIView *)view {
UILabel *label = [[UILabel alloc]init];
label.textAlignment = NSTextAlignmentCenter;
label.font = FONT_14;
label.textColor = [UIColor colorWithHex:0x666666];
if(component ==0) {
CityAreaModel *model = self.provinceArr[row];
label.text = model.name;
NSInteger selectRow = [pickerView selectedRowInComponent:component];
if(selectRow == row) {
label.textColor = [UIColor colorWithHex:0x222222];
}
}
if(component ==1) {
NSInteger selRow = [pickerView selectedRowInComponent:0];
CityAreaModel *model = self.provinceArr[selRow];
CityModel *cityModel = model.children[row];
label.text = cityModel.name;
NSInteger selectRow = [pickerView selectedRowInComponent:component];
if(selectRow == row) {
label.textColor = [UIColor colorWithHex:0x222222];
}
}
if(component ==2) {
NSInteger selRow = [pickerView selectedRowInComponent:0];
CityAreaModel *model = self.provinceArr[selRow];
NSInteger sel1Row = [pickerView selectedRowInComponent:1];
CityModel *cityModel = model.children[sel1Row];
AreaModel *areaModel = cityModel.children[row];
label.text = areaModel.name;
NSInteger selectRow = [pickerView selectedRowInComponent:component];
if(selectRow == row) {
label.textColor = [UIColor colorWithHex:0x222222];
}
}
return label;
}
- (void)clickConfirmButton {
NSInteger selRow = [self.pickerView selectedRowInComponent:0];
CityAreaModel *model = self.provinceArr[selRow];
NSInteger sel1Row = [self.pickerView selectedRowInComponent:1];
CityModel *cityModel = model.children[sel1Row];
NSInteger sel2Row = [self.pickerView selectedRowInComponent:2];
AreaModel *areaModel = cityModel.children[sel2Row];
self.selectCity = model.name;
self.selectProvince = cityModel.name;
self.selectArea = areaModel.name;
if ([self.delegate respondsToSelector:@selector(pickerCityAreaView:selectProvince:selectCity:selectArea:)]) {
[self.delegate pickerCityAreaView:self selectProvince:self.selectProvince selectCity:self.selectCity selectArea:self.selectArea];
}
//为了保留dismiss操作
[super clickConfirmButton];
}
网友评论