| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330 |
- //
- // FDCityPicker.m
- // timePicker
- //
- // Created by xuansa on 2017/1/10.
- // Copyright © 2017年 xuansa. All rights reserved.
- //
- #import "FDCityPicker.h"
- /**
- * 2.返回一个RGBA格式的UIColor对象
- */
- #define RGBA(r, g, b, a) [UIColor colorWithRed:r/255.0f green:g/255.0f blue:b/255.0f alpha:a]
- /**
- * 3.返回一个RGB格式的UIColor对象
- */
- #define RGB(r, g, b) RGBA(r, g, b, 1.0f)
- /**
- * 4.弱引用
- */
- #define STWeakSelf __weak typeof(self) weakSelf = self;
- static CGFloat const PickerViewHeight = 240;
- static CGFloat const PickerViewLabelWeight = 32;
- @interface FDCityPicker ()<UIPickerViewDelegate,UIPickerViewDataSource>
- /** 1.数据源数组 */
- @property (nonatomic, strong, nullable)NSArray *arrayRoot;
- /** 2.当前省数组 */
- @property (nonatomic, strong, nullable)NSMutableArray *arrayProvince;
- /** 3.当前城市数组 */
- @property (nonatomic, strong, nullable)NSMutableArray *arrayCity;
- /** 4.当前地区数组 */
- //@property (nonatomic, strong, nullable)NSMutableArray *arrayArea;
- /** 5.当前选中数组 */
- @property (nonatomic, strong, nullable)NSMutableArray *arraySelected;
- /** 6.选择器 */
- @property (nonatomic, strong, nullable)UIPickerView *pickerView;
- /** 8.边线 */
- @property (nonatomic, strong, nullable)UIView *lineView;
- /** 9.省份 */
- @property (nonatomic, strong, nullable)NSString *province;
- /** 10.城市 */
- @property (nonatomic, strong, nullable)NSString *city;
- /** 11.地区 */
- //@property (nonatomic, strong, nullable)NSString *area;
- @end
- @implementation FDCityPicker
- - (instancetype)initWithDelegate:(id<FDCityPickerDelegate>)delegate{
- self = [self init];
- self.delegate = delegate;
- return self;
- }
- - (instancetype)init{
- self = [super init];
- if (self) {
- [self setupUI];
- [self loadData];
- }
- return self;
- }
- - (void)setupUI{
- self.bounds = [UIScreen mainScreen].bounds;
- self.backgroundColor = RGBA(0, 0, 0, 102.0/255);
- [self.layer setOpaque:0.0];
- [self addSubview:self.pickerView];
- [self.pickerView addSubview:self.lineView];
- [self addSubview:self.toolbar];
- [self addTarget:self action:@selector(remove) forControlEvents:UIControlEventTouchUpInside];
- }
- - (void)loadData{
- [self.arrayRoot enumerateObjectsUsingBlock:^(NSDictionary *obj, NSUInteger idx, BOOL * _Nonnull stop) {
- [self.arrayProvince addObject:obj[@"state"]];
- }];
-
- NSMutableArray *citys = [NSMutableArray arrayWithArray:[self.arrayRoot firstObject][@"cities"]];
- [citys enumerateObjectsUsingBlock:^(NSDictionary *obj, NSUInteger idx, BOOL * _Nonnull stop) {
- [self.arrayCity addObject:obj[@"city"]];
- }];
-
- // self.arrayArea = [citys firstObject][@"area"];
-
- self.province = self.arrayProvince[0];
- self.city = self.arrayCity[0];
- // if (self.arrayArea.count != 0) {
- // self.area = self.arrayArea[0];
- // }else{
- // self.area = @"";
- // }
- }
- - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
- {
- return 2;
- }
- - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
- {
- if (component == 0) {
- return self.arrayProvince.count;
- }else{
- return self.arrayCity.count;
- // }else{
- // // return self.arrayArea.count;
- // }
- }
- }
- - (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component
- {
- return PickerViewLabelWeight;
- }
- - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
- {
- if (component == 0) {
- self.arraySelected = self.arrayRoot[row][@"cities"];
-
- [self.arrayCity removeAllObjects];
- [self.arraySelected enumerateObjectsUsingBlock:^(NSDictionary *obj, NSUInteger idx, BOOL * _Nonnull stop) {
- [self.arrayCity addObject:obj[@"city"]];
- }];
-
- // self.arrayArea = [NSMutableArray arrayWithArray:[self.arraySelected firstObject][@"areas"]];
-
- [pickerView reloadComponent:1];
- // [pickerView reloadComponent:2];
- [pickerView selectRow:0 inComponent:1 animated:YES];
- // [pickerView selectRow:0 inComponent:2 animated:YES];
-
- }else{
- if (self.arraySelected.count == 0) {
- self.arraySelected = [self.arrayRoot firstObject][@"cities"];
- }
-
- // self.arrayArea = [NSMutableArray arrayWithArray:[self.arraySelected objectAtIndex:row][@"areas"]];
-
- // [pickerView reloadComponent:2];
- // [pickerView selectRow:0 inComponent:2 animated:YES];
-
- }
-
- [self reloadData];
- }
- - (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(nullable UIView *)view
- {
-
- NSString *text;
- if (component == 0) {
- text = self.arrayProvince[row];
- }else if (component == 1){
- text = self.arrayCity[row];
- }else{
- // if (self.arrayArea.count > 0) {
- // text = self.arrayArea[row];
- // }else{
- // text = @"";
- // }
- }
-
-
- UILabel *label = [[UILabel alloc]init];
- [label setTextAlignment:NSTextAlignmentCenter];
- [label setFont:[UIFont systemFontOfSize:17]];
- [label setText:text];
- return label;
-
-
- }
- - (void)selectedOk {
- [self.delegate pickerAreaer:self province:self.province city:self.city row:self.row];
- [self remove];
- }
- - (void)selectedCancel
- {
- [self remove];
- }
- #pragma mark - --- private methods 私有方法 ---
- - (void)reloadData
- {
- NSInteger index0 = [self.pickerView selectedRowInComponent:0];
- NSInteger index1 = [self.pickerView selectedRowInComponent:1];
- // NSInteger index2 = [self.pickerView selectedRowInComponent:2];
- self.province = self.arrayProvince[index0];
- self.city = self.arrayCity[index1];
- // if (self.arrayArea.count != 0) {
- // self.area = self.arrayArea[index2];
- // }else{
- // self.area = @"";
- // }
-
- NSString *title = [NSString stringWithFormat:@"%@ %@ ", self.province, self.city];
- [self.toolbar setTitle:title];
-
- }
- - (void)show
- {
- [[UIApplication sharedApplication].keyWindow addSubview:self];
- [self setCenter:[UIApplication sharedApplication].keyWindow.center];
- [[UIApplication sharedApplication].keyWindow bringSubviewToFront:self];
-
- CGRect frameTool = self.toolbar.frame;
- frameTool.origin.y -= PickerViewHeight;
-
- CGRect framePicker = self.pickerView.frame;
- framePicker.origin.y -= PickerViewHeight;
- [UIView animateWithDuration:0.33 animations:^{
- [self.layer setOpacity:1];
- self.toolbar.frame = frameTool;
- self.pickerView.frame = framePicker;
- } completion:^(BOOL finished) {
- }];
- }
- - (void)remove
- {
- CGRect frameTool = self.toolbar.frame;
- frameTool.origin.y += PickerViewHeight;
-
- CGRect framePicker = self.pickerView.frame;
- framePicker.origin.y += PickerViewHeight;
- [UIView animateWithDuration:0.33 animations:^{
- [self.layer setOpacity:0];
- self.toolbar.frame = frameTool;
- self.pickerView.frame = framePicker;
- } completion:^(BOOL finished) {
- [self removeFromSuperview];
- }];
- }
- #pragma mark - --- setters 属性 ---
- #pragma mark - --- getters 属性 ---
- - (NSArray *)arrayRoot
- {
- if (!_arrayRoot) {
- NSString *path = [[NSBundle mainBundle]pathForResource:@"area" ofType:@"plist"];
- _arrayRoot = [[NSArray alloc]initWithContentsOfFile:path];
- }
- return _arrayRoot;
- }
- - (NSMutableArray *)arrayProvince
- {
- if (!_arrayProvince) {
- _arrayProvince = [NSMutableArray array];
- }
- return _arrayProvince;
- }
- - (NSMutableArray *)arrayCity
- {
- if (!_arrayCity) {
- _arrayCity = [NSMutableArray array];
- }
- return _arrayCity;
- }
- //- (NSMutableArray *)arrayArea
- //{
- //// if (!_arrayArea) {
- //// _arrayArea = [NSMutableArray array];
- //// }
- //// return _arrayArea;
- //}
- - (NSMutableArray *)arraySelected
- {
- if (!_arraySelected) {
- _arraySelected = [NSMutableArray array];
- }
- return _arraySelected;
- }
- - (UIPickerView *)pickerView
- {
- if (!_pickerView) {
- CGFloat pickerW = CGRectGetWidth([UIScreen mainScreen].bounds);
- CGFloat pickerH = PickerViewHeight - 44;
- CGFloat pickerX = 0;
- CGFloat pickerY = CGRectGetHeight([UIScreen mainScreen].bounds)+44;
- _pickerView = [[UIPickerView alloc]initWithFrame:CGRectMake(pickerX, pickerY, pickerW, pickerH)];
- [_pickerView setBackgroundColor:[UIColor whiteColor]];
- [_pickerView setDataSource:self];
- [_pickerView setDelegate:self];
- }
- return _pickerView;
- }
- - (FDTool *)toolbar {
- if (!_toolbar) {
- _toolbar = [[FDTool alloc]initWithTitle:@"选择城市地区"
- cancelButtonTitle:@"取消"
- okButtonTitle:@"确定"
- addTarget:self
- cancelAction:@selector(selectedCancel)
- okAction:@selector(selectedOk)];
- _toolbar.x = 0;
- _toolbar.y = CGRectGetHeight([UIScreen mainScreen].bounds);
- _toolbar.buttonTitleColor = NewNavigationColor;
- _toolbar.buttonBackgroundColor = [UIColor whiteColor];
- }
- return _toolbar;
- }
- - (UIView *)lineView
- {
- if (!_lineView) {
- _lineView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, CGRectGetWidth([UIScreen mainScreen].bounds), 0.5)];
- [_lineView setBackgroundColor:RGB(205, 205, 205)];
- }
- return _lineView;
- }
- @end
|