FDCityPicker.m 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. //
  2. // FDCityPicker.m
  3. // timePicker
  4. //
  5. // Created by xuansa on 2017/1/10.
  6. // Copyright © 2017年 xuansa. All rights reserved.
  7. //
  8. #import "FDCityPicker.h"
  9. /**
  10. * 2.返回一个RGBA格式的UIColor对象
  11. */
  12. #define RGBA(r, g, b, a) [UIColor colorWithRed:r/255.0f green:g/255.0f blue:b/255.0f alpha:a]
  13. /**
  14. * 3.返回一个RGB格式的UIColor对象
  15. */
  16. #define RGB(r, g, b) RGBA(r, g, b, 1.0f)
  17. /**
  18. * 4.弱引用
  19. */
  20. #define STWeakSelf __weak typeof(self) weakSelf = self;
  21. static CGFloat const PickerViewHeight = 240;
  22. static CGFloat const PickerViewLabelWeight = 32;
  23. @interface FDCityPicker ()<UIPickerViewDelegate,UIPickerViewDataSource>
  24. /** 1.数据源数组 */
  25. @property (nonatomic, strong, nullable)NSArray *arrayRoot;
  26. /** 2.当前省数组 */
  27. @property (nonatomic, strong, nullable)NSMutableArray *arrayProvince;
  28. /** 3.当前城市数组 */
  29. @property (nonatomic, strong, nullable)NSMutableArray *arrayCity;
  30. /** 4.当前地区数组 */
  31. //@property (nonatomic, strong, nullable)NSMutableArray *arrayArea;
  32. /** 5.当前选中数组 */
  33. @property (nonatomic, strong, nullable)NSMutableArray *arraySelected;
  34. /** 6.选择器 */
  35. @property (nonatomic, strong, nullable)UIPickerView *pickerView;
  36. /** 8.边线 */
  37. @property (nonatomic, strong, nullable)UIView *lineView;
  38. /** 9.省份 */
  39. @property (nonatomic, strong, nullable)NSString *province;
  40. /** 10.城市 */
  41. @property (nonatomic, strong, nullable)NSString *city;
  42. /** 11.地区 */
  43. //@property (nonatomic, strong, nullable)NSString *area;
  44. @end
  45. @implementation FDCityPicker
  46. - (instancetype)initWithDelegate:(id<FDCityPickerDelegate>)delegate{
  47. self = [self init];
  48. self.delegate = delegate;
  49. return self;
  50. }
  51. - (instancetype)init{
  52. self = [super init];
  53. if (self) {
  54. [self setupUI];
  55. [self loadData];
  56. }
  57. return self;
  58. }
  59. - (void)setupUI{
  60. self.bounds = [UIScreen mainScreen].bounds;
  61. self.backgroundColor = RGBA(0, 0, 0, 102.0/255);
  62. [self.layer setOpaque:0.0];
  63. [self addSubview:self.pickerView];
  64. [self.pickerView addSubview:self.lineView];
  65. [self addSubview:self.toolbar];
  66. [self addTarget:self action:@selector(remove) forControlEvents:UIControlEventTouchUpInside];
  67. }
  68. - (void)loadData{
  69. [self.arrayRoot enumerateObjectsUsingBlock:^(NSDictionary *obj, NSUInteger idx, BOOL * _Nonnull stop) {
  70. [self.arrayProvince addObject:obj[@"state"]];
  71. }];
  72. NSMutableArray *citys = [NSMutableArray arrayWithArray:[self.arrayRoot firstObject][@"cities"]];
  73. [citys enumerateObjectsUsingBlock:^(NSDictionary *obj, NSUInteger idx, BOOL * _Nonnull stop) {
  74. [self.arrayCity addObject:obj[@"city"]];
  75. }];
  76. // self.arrayArea = [citys firstObject][@"area"];
  77. self.province = self.arrayProvince[0];
  78. self.city = self.arrayCity[0];
  79. // if (self.arrayArea.count != 0) {
  80. // self.area = self.arrayArea[0];
  81. // }else{
  82. // self.area = @"";
  83. // }
  84. }
  85. - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
  86. {
  87. return 2;
  88. }
  89. - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
  90. {
  91. if (component == 0) {
  92. return self.arrayProvince.count;
  93. }else{
  94. return self.arrayCity.count;
  95. // }else{
  96. // // return self.arrayArea.count;
  97. // }
  98. }
  99. }
  100. - (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component
  101. {
  102. return PickerViewLabelWeight;
  103. }
  104. - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
  105. {
  106. if (component == 0) {
  107. self.arraySelected = self.arrayRoot[row][@"cities"];
  108. [self.arrayCity removeAllObjects];
  109. [self.arraySelected enumerateObjectsUsingBlock:^(NSDictionary *obj, NSUInteger idx, BOOL * _Nonnull stop) {
  110. [self.arrayCity addObject:obj[@"city"]];
  111. }];
  112. // self.arrayArea = [NSMutableArray arrayWithArray:[self.arraySelected firstObject][@"areas"]];
  113. [pickerView reloadComponent:1];
  114. // [pickerView reloadComponent:2];
  115. [pickerView selectRow:0 inComponent:1 animated:YES];
  116. // [pickerView selectRow:0 inComponent:2 animated:YES];
  117. }else{
  118. if (self.arraySelected.count == 0) {
  119. self.arraySelected = [self.arrayRoot firstObject][@"cities"];
  120. }
  121. // self.arrayArea = [NSMutableArray arrayWithArray:[self.arraySelected objectAtIndex:row][@"areas"]];
  122. // [pickerView reloadComponent:2];
  123. // [pickerView selectRow:0 inComponent:2 animated:YES];
  124. }
  125. [self reloadData];
  126. }
  127. - (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(nullable UIView *)view
  128. {
  129. NSString *text;
  130. if (component == 0) {
  131. text = self.arrayProvince[row];
  132. }else if (component == 1){
  133. text = self.arrayCity[row];
  134. }else{
  135. // if (self.arrayArea.count > 0) {
  136. // text = self.arrayArea[row];
  137. // }else{
  138. // text = @"";
  139. // }
  140. }
  141. UILabel *label = [[UILabel alloc]init];
  142. [label setTextAlignment:NSTextAlignmentCenter];
  143. [label setFont:[UIFont systemFontOfSize:17]];
  144. [label setText:text];
  145. return label;
  146. }
  147. - (void)selectedOk {
  148. [self.delegate pickerAreaer:self province:self.province city:self.city row:self.row];
  149. [self remove];
  150. }
  151. - (void)selectedCancel
  152. {
  153. [self remove];
  154. }
  155. #pragma mark - --- private methods 私有方法 ---
  156. - (void)reloadData
  157. {
  158. NSInteger index0 = [self.pickerView selectedRowInComponent:0];
  159. NSInteger index1 = [self.pickerView selectedRowInComponent:1];
  160. // NSInteger index2 = [self.pickerView selectedRowInComponent:2];
  161. self.province = self.arrayProvince[index0];
  162. self.city = self.arrayCity[index1];
  163. // if (self.arrayArea.count != 0) {
  164. // self.area = self.arrayArea[index2];
  165. // }else{
  166. // self.area = @"";
  167. // }
  168. NSString *title = [NSString stringWithFormat:@"%@ %@ ", self.province, self.city];
  169. [self.toolbar setTitle:title];
  170. }
  171. - (void)show
  172. {
  173. [[UIApplication sharedApplication].keyWindow addSubview:self];
  174. [self setCenter:[UIApplication sharedApplication].keyWindow.center];
  175. [[UIApplication sharedApplication].keyWindow bringSubviewToFront:self];
  176. CGRect frameTool = self.toolbar.frame;
  177. frameTool.origin.y -= PickerViewHeight;
  178. CGRect framePicker = self.pickerView.frame;
  179. framePicker.origin.y -= PickerViewHeight;
  180. [UIView animateWithDuration:0.33 animations:^{
  181. [self.layer setOpacity:1];
  182. self.toolbar.frame = frameTool;
  183. self.pickerView.frame = framePicker;
  184. } completion:^(BOOL finished) {
  185. }];
  186. }
  187. - (void)remove
  188. {
  189. CGRect frameTool = self.toolbar.frame;
  190. frameTool.origin.y += PickerViewHeight;
  191. CGRect framePicker = self.pickerView.frame;
  192. framePicker.origin.y += PickerViewHeight;
  193. [UIView animateWithDuration:0.33 animations:^{
  194. [self.layer setOpacity:0];
  195. self.toolbar.frame = frameTool;
  196. self.pickerView.frame = framePicker;
  197. } completion:^(BOOL finished) {
  198. [self removeFromSuperview];
  199. }];
  200. }
  201. #pragma mark - --- setters 属性 ---
  202. #pragma mark - --- getters 属性 ---
  203. - (NSArray *)arrayRoot
  204. {
  205. if (!_arrayRoot) {
  206. NSString *path = [[NSBundle mainBundle]pathForResource:@"area" ofType:@"plist"];
  207. _arrayRoot = [[NSArray alloc]initWithContentsOfFile:path];
  208. }
  209. return _arrayRoot;
  210. }
  211. - (NSMutableArray *)arrayProvince
  212. {
  213. if (!_arrayProvince) {
  214. _arrayProvince = [NSMutableArray array];
  215. }
  216. return _arrayProvince;
  217. }
  218. - (NSMutableArray *)arrayCity
  219. {
  220. if (!_arrayCity) {
  221. _arrayCity = [NSMutableArray array];
  222. }
  223. return _arrayCity;
  224. }
  225. //- (NSMutableArray *)arrayArea
  226. //{
  227. //// if (!_arrayArea) {
  228. //// _arrayArea = [NSMutableArray array];
  229. //// }
  230. //// return _arrayArea;
  231. //}
  232. - (NSMutableArray *)arraySelected
  233. {
  234. if (!_arraySelected) {
  235. _arraySelected = [NSMutableArray array];
  236. }
  237. return _arraySelected;
  238. }
  239. - (UIPickerView *)pickerView
  240. {
  241. if (!_pickerView) {
  242. CGFloat pickerW = CGRectGetWidth([UIScreen mainScreen].bounds);
  243. CGFloat pickerH = PickerViewHeight - 44;
  244. CGFloat pickerX = 0;
  245. CGFloat pickerY = CGRectGetHeight([UIScreen mainScreen].bounds)+44;
  246. _pickerView = [[UIPickerView alloc]initWithFrame:CGRectMake(pickerX, pickerY, pickerW, pickerH)];
  247. [_pickerView setBackgroundColor:[UIColor whiteColor]];
  248. [_pickerView setDataSource:self];
  249. [_pickerView setDelegate:self];
  250. }
  251. return _pickerView;
  252. }
  253. - (FDTool *)toolbar {
  254. if (!_toolbar) {
  255. _toolbar = [[FDTool alloc]initWithTitle:@"选择城市地区"
  256. cancelButtonTitle:@"取消"
  257. okButtonTitle:@"确定"
  258. addTarget:self
  259. cancelAction:@selector(selectedCancel)
  260. okAction:@selector(selectedOk)];
  261. _toolbar.x = 0;
  262. _toolbar.y = CGRectGetHeight([UIScreen mainScreen].bounds);
  263. _toolbar.buttonTitleColor = NewNavigationColor;
  264. _toolbar.buttonBackgroundColor = [UIColor whiteColor];
  265. }
  266. return _toolbar;
  267. }
  268. - (UIView *)lineView
  269. {
  270. if (!_lineView) {
  271. _lineView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, CGRectGetWidth([UIScreen mainScreen].bounds), 0.5)];
  272. [_lineView setBackgroundColor:RGB(205, 205, 205)];
  273. }
  274. return _lineView;
  275. }
  276. @end