YLAreaPickerView.m 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  1. //
  2. // YLAreaPickerView.m
  3. // LiveTV
  4. //
  5. // Created by 翟玉磊 on 2019/4/22.
  6. // Copyright © 2019 翟玉磊. All rights reserved.
  7. //
  8. #import "YLAreaPickerView.h"
  9. #import "ProvincOrCityOrAreaModel.h"
  10. static const NSInteger KProvinceComponent = 0;
  11. static const NSInteger KCityComponent = 1;
  12. static const NSInteger KDistrictComponent = 2;
  13. #define KFont14 [UIFont systemFontOfSize:14]
  14. @interface YLAreaPickerView ()<UIPickerViewDataSource, UIPickerViewDelegate>
  15. {
  16. NSInteger _selectRow0;//记录第0个轮子的选择行
  17. NSInteger _selectRow1;
  18. NSInteger _selectRow2;
  19. NSString *_areaString;//最后要传回的详细地址拼接字符串
  20. NSDictionary *_infoDic;
  21. ProvincOrCityOrAreaModel *_proModel;//记录下选中省的数据
  22. ProvincOrCityOrAreaModel *_cityModel;
  23. ProvincOrCityOrAreaModel *_areaModel;
  24. }
  25. @property (nonatomic, readwrite, strong) UIButton *bgView;
  26. @property (nonatomic, readwrite, strong) UIView *infoView;
  27. @property (nonatomic, readwrite, strong) UIView *titleView;
  28. @property (nonatomic, readwrite, strong) UILabel *titleLabel;
  29. @property (nonatomic, readwrite, strong) UIButton *cancelButton;
  30. @property (nonatomic, readwrite, strong) UIButton *sureButton;
  31. @property (strong, nonatomic) UIPickerView *pickerView;
  32. @property (strong, nonatomic) ProvincOrCityOrAreaModel *provBridge;
  33. @property (strong, nonatomic) ProvincOrCityOrAreaModel *cityBridge;
  34. @property (strong, nonatomic) ProvincOrCityOrAreaModel *areaBridge;
  35. @property (strong, nonatomic) NSMutableArray *provDataArr;//省数组
  36. @property (strong, nonatomic) NSMutableArray *cityDataArr;//市数组
  37. @property (strong, nonatomic) NSMutableArray * areaDataArr;//区数组
  38. @property (nonatomic, readwrite, copy) NSString *selectProvId;
  39. @property (nonatomic, readwrite, copy) NSString *selectCityId;
  40. @property (nonatomic, readwrite, copy) NSString *selectAreaId;
  41. @end
  42. @implementation YLAreaPickerView
  43. #pragma mark - Public
  44. + (instancetype)areaPickerWithTitle:(NSString *)title target:(id<YLAreaPickerViewDelegate>)target {
  45. YLAreaPickerView *picker = [[YLAreaPickerView alloc] initWithFrame:[UIScreen mainScreen].bounds];
  46. picker.titleLabel.text = title;
  47. picker.delegate = target;
  48. [[AppDelegate sharedDelegate].window addSubview:picker];
  49. return picker;
  50. }
  51. - (void)show {
  52. self.hidden = NO;
  53. [UIView animateWithDuration:.3 animations:^{
  54. self.infoView.frame = CGRectMake(0, self.f_heigh - 264.0f, self.f_width, 264.0f);
  55. } completion:^(BOOL finished) {
  56. }];
  57. }
  58. - (void)dismiss {
  59. [UIView animateWithDuration:.3 animations:^{
  60. self.infoView.frame = CGRectMake(0, self.f_heigh, self.f_width, 264.0f);
  61. } completion:^(BOOL finished) {
  62. self.hidden = YES;
  63. [self removeFromSuperview];
  64. }];
  65. }
  66. - (void)setupCityWithAreaPath:(NSString *)areaPath {
  67. NSArray *areaIds = [areaPath componentsSeparatedByString:@","];
  68. if (areaIds.count == 0) {
  69. [self downloadProvWithAreaId:@""];
  70. return;
  71. }
  72. for (NSInteger i = 0; i < areaIds.count; i++) {
  73. NSString *areaId = [areaIds objectAtIndex:i];
  74. if (StringIsNotEmpty(areaId)) {
  75. switch (i) {
  76. case 0:
  77. self.selectProvId = areaId;
  78. break;
  79. case 1:
  80. self.selectCityId = areaId;
  81. break;
  82. case 2:
  83. self.selectAreaId = areaId;
  84. break;
  85. default:
  86. break;
  87. }
  88. }
  89. }
  90. [self downloadProvWithAreaId:self.selectProvId];
  91. }
  92. - (instancetype)initWithFrame:(CGRect)frame {
  93. self = [super initWithFrame:frame];
  94. if (self) {
  95. [self _setupView];
  96. }
  97. return self;
  98. }
  99. - (void)_setupView {
  100. _selectRow0 = 0;
  101. _selectRow1 = 0;
  102. _selectRow2 = 0;
  103. self.provDataArr = [[NSMutableArray alloc]init];
  104. self.cityDataArr = [[NSMutableArray alloc]init];
  105. self.areaDataArr = [[NSMutableArray alloc]init];
  106. [self addSubview:self.bgView];
  107. [self addSubview:self.infoView];
  108. [self.infoView addSubview:self.titleView];
  109. [self.infoView addSubview:self.pickerView];
  110. [self.titleView addSubview:self.cancelButton];
  111. [self.titleView addSubview:self.titleLabel];
  112. [self.titleView addSubview:self.sureButton];
  113. self.bgView.frame = self.bounds;
  114. self.infoView.frame = CGRectMake(0, self.f_heigh, self.f_width, 264.0f);
  115. [self.titleView mas_makeConstraints:^(MASConstraintMaker *make) {
  116. make.left.top.right.equalTo(self.infoView);
  117. make.height.equalTo(@44.0f);
  118. }];
  119. [self.pickerView mas_makeConstraints:^(MASConstraintMaker *make) {
  120. make.left.right.bottom.equalTo(self.infoView);
  121. make.top.equalTo(self.titleView.mas_bottom);
  122. }];
  123. [self.cancelButton mas_makeConstraints:^(MASConstraintMaker *make) {
  124. make.left.equalTo(self.titleView).offset(10.0f);
  125. make.width.height.equalTo(@44.0f);
  126. make.centerY.equalTo(self.titleView);
  127. }];
  128. [self.sureButton mas_makeConstraints:^(MASConstraintMaker *make) {
  129. make.right.equalTo(self.titleView).offset(-10.0f);
  130. make.width.height.equalTo(@44.0f);
  131. make.centerY.equalTo(self.titleView);
  132. }];
  133. [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  134. make.left.equalTo(self.cancelButton).offset(10.0f);
  135. make.right.equalTo(self.sureButton).offset(-10.0f);
  136. make.centerY.equalTo(self.titleView);
  137. }];
  138. [self.bgView addTarget:self action:@selector(selectAddressClose) forControlEvents:UIControlEventTouchUpInside];
  139. [self.cancelButton addTarget:self action:@selector(selectAddressClose) forControlEvents:UIControlEventTouchUpInside];
  140. [self.sureButton addTarget:self action:@selector(selectAddressComplete) forControlEvents:UIControlEventTouchUpInside];
  141. self.hidden = YES;
  142. }
  143. #pragma mark - http methods
  144. - (void)downloadProvWithAreaId:(NSString *)areaId {
  145. [[XYConfigAPIManager new] getAreaListWithParentId:nil successHandler:^(ZYLResponseModel *responseModel) {
  146. [self.provDataArr removeAllObjects];
  147. if (responseModel.codeState) {
  148. NSMutableArray *temp = [NSMutableArray array];
  149. for (NSDictionary *dict in responseModel.result[@"data"][@"list"]) {
  150. ProvincOrCityOrAreaModel *model = [ProvincOrCityOrAreaModel new];
  151. [model yy_modelSetWithDictionary:dict];
  152. [temp addObject:model];
  153. }
  154. [self.provDataArr addObjectsFromArray:temp];
  155. if (StringIsEmpty(areaId)) {
  156. [self pickerView:self.pickerView didSelectRow:0 inComponent:0 ];
  157. [self.pickerView reloadAllComponents];
  158. }else {
  159. for (NSInteger i = 0; i < self.provDataArr.count; i++) {
  160. ProvincOrCityOrAreaModel *model = self.provDataArr[i];
  161. if ([model.areaId isEqualToString:areaId]) {
  162. self->_proModel = model;
  163. [self.pickerView reloadAllComponents];
  164. [self.pickerView selectRow:i inComponent:KProvinceComponent animated:YES];
  165. [self downloadCityWithId:self.selectProvId selectedAreaId:self.selectCityId];
  166. break;
  167. }
  168. }
  169. }
  170. }
  171. } failureHandler:^(NSError *error) {
  172. }];
  173. }
  174. - (void)downloadCityWithId:(NSString *)provId selectedAreaId:(NSString *)selectedAreaId {
  175. [[XYConfigAPIManager new] getAreaListWithParentId:provId successHandler:^(ZYLResponseModel *responseModel) {
  176. [self.cityDataArr removeAllObjects];
  177. if (responseModel.codeState) {
  178. NSMutableArray *temp = [NSMutableArray array];
  179. for (NSDictionary *dict in responseModel.result[@"data"][@"list"]) {
  180. ProvincOrCityOrAreaModel *model = [ProvincOrCityOrAreaModel new];
  181. [model yy_modelSetWithDictionary:dict];
  182. [temp addObject:model];
  183. }
  184. [self.cityDataArr addObjectsFromArray:temp];
  185. if (StringIsEmpty(selectedAreaId)) {
  186. [self.pickerView reloadComponent:1];
  187. [self.pickerView selectRow:0 inComponent:1 animated:YES];
  188. [self pickerView:self.pickerView didSelectRow:0 inComponent:1 ];
  189. self->_cityModel = self.cityDataArr[self->_selectRow1];
  190. [self downloadAreaWithId:self->_cityModel.areaId selectedAreaId:@""];
  191. }else {
  192. for (NSInteger i = 0; i < self.cityDataArr.count; i++) {
  193. ProvincOrCityOrAreaModel *model = self.cityDataArr[i];
  194. if ([model.areaId isEqualToString:selectedAreaId]) {
  195. self->_cityModel = model;
  196. self->_infoDic = @{@"proID":self->_proModel.areaId,
  197. @"cityId":self->_cityModel.areaId,
  198. @"areaName":self->_cityModel.areaName,
  199. };
  200. self->_areaString = [NSString stringWithFormat:@"%@%@",self->_proModel.areaName,self->_cityModel.areaName];
  201. [self.pickerView reloadAllComponents];
  202. [self.pickerView selectRow:i inComponent:KCityComponent animated:YES];
  203. [self downloadAreaWithId:self.selectCityId selectedAreaId:self.selectAreaId];
  204. break;
  205. }
  206. }
  207. }
  208. }
  209. } failureHandler:^(NSError *error) {
  210. }];
  211. }
  212. - (void)downloadAreaWithId:(NSString *)cityId selectedAreaId:(NSString *)selectedAreaId {
  213. [[XYConfigAPIManager new] getAreaListWithParentId:cityId successHandler:^(ZYLResponseModel *responseModel) {
  214. [self.areaDataArr removeAllObjects];
  215. if (responseModel.codeState) {
  216. NSMutableArray *temp = [NSMutableArray array];
  217. for (NSDictionary *dict in responseModel.result[@"data"][@"list"]) {
  218. ProvincOrCityOrAreaModel *model = [ProvincOrCityOrAreaModel new];
  219. [model yy_modelSetWithDictionary:dict];
  220. [temp addObject:model];
  221. }
  222. [self.areaDataArr addObjectsFromArray:temp];
  223. if (StringIsEmpty(selectedAreaId)) {
  224. [self.pickerView reloadComponent:2];
  225. [self.pickerView selectRow:0 inComponent:2 animated:YES];
  226. [self pickerView:self.pickerView didSelectRow:0 inComponent:2];
  227. }else {
  228. for (NSInteger i = 0; i < self.areaDataArr.count; i++) {
  229. ProvincOrCityOrAreaModel *model = self.areaDataArr[i];
  230. if ([model.areaId isEqualToString:selectedAreaId]) {
  231. self->_areaModel = model;
  232. self->_infoDic = @{@"proID":self->_proModel.areaId,
  233. @"cityId":self->_cityModel.areaId,
  234. @"areaId":self->_areaModel.areaId,
  235. @"areaName":self->_areaModel.areaName,
  236. };
  237. self->_areaString = [NSString stringWithFormat:@"%@%@%@",self->_proModel.areaName,self->_cityModel.areaName,self->_areaModel.areaName];
  238. [self.pickerView reloadAllComponents];
  239. [self.pickerView selectRow:i inComponent:KDistrictComponent animated:YES];
  240. break;
  241. }
  242. }
  243. }
  244. }
  245. } failureHandler:^(NSError *error) {
  246. }];
  247. }
  248. #pragma mark - events response
  249. - (void)selectAddressComplete {
  250. if (self.delegate && [self.delegate respondsToSelector:@selector(addressPickerViewDidSelected:withDic:)]) {
  251. [self.delegate addressPickerViewDidSelected:_areaString withDic:_infoDic];
  252. }
  253. [self dismiss];
  254. }
  255. - (void)selectAddressClose {
  256. if (self.delegate && [self.delegate respondsToSelector:@selector(addressPickerViewDidClose)]) {
  257. [self.delegate addressPickerViewDidClose];
  258. }
  259. [self dismiss];
  260. }
  261. #pragma mark - UIPickerViewDataSource
  262. - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
  263. return 3;
  264. }
  265. - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
  266. if (component==0) {
  267. return self.provDataArr.count;
  268. }else if(component==1){
  269. return self.cityDataArr.count;
  270. }else{
  271. return self.areaDataArr.count;
  272. }
  273. }
  274. - (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component{
  275. return 40;
  276. }
  277. - (NSAttributedString *)pickerView:(UIPickerView *)pickerView attributedTitleForRow:(NSInteger)row forComponent:(NSInteger)component{
  278. NSDictionary * attrDic = @{NSForegroundColorAttributeName:ColorFromHexString(@"#453738"),NSFontAttributeName:Font_B(16)};
  279. ProvincOrCityOrAreaModel *provice = [[ProvincOrCityOrAreaModel alloc]init];
  280. ProvincOrCityOrAreaModel *city = [[ProvincOrCityOrAreaModel alloc]init];
  281. ProvincOrCityOrAreaModel *area = [[ProvincOrCityOrAreaModel alloc]init];
  282. NSAttributedString * attrString;
  283. if (component==0) {
  284. provice = (ProvincOrCityOrAreaModel *)self.provDataArr[row];
  285. attrString = [[NSAttributedString alloc] initWithString:provice.areaName attributes:attrDic];
  286. }else if(component==1){
  287. city = (ProvincOrCityOrAreaModel *)self.cityDataArr[row];
  288. attrString = [[NSAttributedString alloc] initWithString:city.areaName attributes:attrDic];
  289. }else{
  290. area = (ProvincOrCityOrAreaModel *)self.areaDataArr[row];
  291. attrString = [[NSAttributedString alloc] initWithString:area.areaName attributes:attrDic];
  292. }
  293. return attrString;
  294. }
  295. - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
  296. if (component==0) {
  297. _selectRow0 = [pickerView selectedRowInComponent:0];
  298. _selectRow1 = 0;
  299. _selectRow2 = 0;
  300. _proModel = self.provDataArr[_selectRow0];
  301. [self downloadCityWithId:_proModel.areaId selectedAreaId:@""];
  302. }else if(component==1){
  303. _selectRow1 = [pickerView selectedRowInComponent:1];
  304. _selectRow2 = 0;
  305. _cityModel = self.cityDataArr[_selectRow1];
  306. [self downloadAreaWithId:_cityModel.areaId selectedAreaId:@""];
  307. }else{
  308. _selectRow2 = [pickerView selectedRowInComponent:2];
  309. if (self.areaDataArr&&self.areaDataArr.count>0) {
  310. _areaModel = self.areaDataArr[_selectRow2];
  311. }else{
  312. _areaModel = nil;
  313. }
  314. }
  315. if (_areaModel==nil) {
  316. _areaString = [NSString stringWithFormat:@"%@%@",_proModel.areaName,_cityModel.areaName];
  317. if (_proModel && _cityModel) {
  318. _infoDic = @{@"proID":_proModel.areaId,
  319. @"cityId":_cityModel.areaId,
  320. @"areaName":_cityModel.areaName,
  321. };
  322. }
  323. }else{
  324. _areaString = [NSString stringWithFormat:@"%@%@%@",_proModel.areaName,_cityModel.areaName,_areaModel.areaName];
  325. if (_proModel && _cityModel && _areaModel) {
  326. _infoDic = @{@"proID":_proModel.areaId,
  327. @"cityId":_cityModel.areaId,
  328. @"areaId":_areaModel.areaId,
  329. @"areaName":_areaModel.areaName,
  330. };
  331. }
  332. }
  333. [pickerView reloadAllComponents];
  334. }
  335. - (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {
  336. for(UIView *singleLine in pickerView.subviews)
  337. {
  338. if (singleLine.frame.size.height < 1)
  339. {
  340. singleLine.backgroundColor = Color_line;
  341. singleLine.frame = CGRectMake(20.0f, singleLine.f_y, pickerView.f_width - 40.0f, singleLine.f_heigh);
  342. }
  343. }
  344. CGFloat subViewW = (pickerView.frame.size.width - 40.0f) / 3;
  345. CGFloat subViewH = 44.0f;
  346. UIView *subView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, subViewW, subViewH)];
  347. subView.backgroundColor = Color_White;
  348. UILabel *myView = [[UILabel alloc] initWithFrame:subView.frame];;
  349. myView.backgroundColor = [UIColor clearColor];
  350. myView.textAlignment = NSTextAlignmentCenter;
  351. if (component == KProvinceComponent) {
  352. self.provBridge = [_provDataArr objectAtIndex:row];
  353. myView.text = self.provBridge.areaName;
  354. if (_selectRow0 == row) {
  355. myView.textColor = ColorFromHexString(@"#453738");
  356. myView.font = Font_B(18);
  357. }else {
  358. myView.textColor = ColorFromHexString(@"#453738");
  359. myView.font = Font_B(16);
  360. }
  361. }
  362. else if (component == KCityComponent) {
  363. self.cityBridge = [_cityDataArr objectAtIndex:row];
  364. myView.text = self.cityBridge.areaName;
  365. if (_selectRow1 == row) {
  366. myView.textColor = ColorFromHexString(@"#453738");
  367. myView.font = Font_B(18);
  368. }else {
  369. myView.textColor = ColorFromHexString(@"#453738");
  370. myView.font = Font_B(16);
  371. }
  372. }
  373. else {
  374. self.areaBridge = [_areaDataArr objectAtIndex:row];
  375. myView.text = self.areaBridge.areaName;
  376. if (_selectRow2 == row) {
  377. myView.textColor = ColorFromHexString(@"#453738");
  378. myView.font = Font_B(18);
  379. }else {
  380. myView.textColor = ColorFromHexString(@"#453738");
  381. myView.font = Font_B(16);
  382. }
  383. }
  384. [subView addSubview:myView];
  385. return subView;
  386. }
  387. #pragma mark - getters and setters
  388. - (UIButton *)bgView {
  389. if (_bgView == nil) {
  390. _bgView = [UIButton buttonWithType:UIButtonTypeCustom];
  391. _bgView.backgroundColor = ColorFromHexString(@"#453738");
  392. _bgView.alpha = 0.4f;
  393. }
  394. return _bgView;
  395. }
  396. - (UIView *)infoView {
  397. if (_infoView == nil) {
  398. _infoView = [UIView new];
  399. _infoView.backgroundColor = Color_White;
  400. }
  401. return _infoView;
  402. }
  403. - (UIView *)titleView {
  404. if (_titleView == nil) {
  405. _titleView = [[UIView alloc] init];
  406. _titleView.backgroundColor = ColorFromHexString(@"#FAFAFA");
  407. }
  408. return _titleView;
  409. }
  410. - (UIButton *)cancelButton {
  411. if (_cancelButton == nil) {
  412. _cancelButton = [UIButton buttonWithType:UIButtonTypeCustom];
  413. [_cancelButton setTitle:kLocalizedString(@"取消") forState:UIControlStateNormal];
  414. [_cancelButton setTitleColor:ColorFromHexString(@"#999999") forState:UIControlStateNormal];
  415. _cancelButton.titleLabel.font = Font(16);
  416. }
  417. return _cancelButton;
  418. }
  419. - (UIButton *)sureButton {
  420. if (_sureButton == nil) {
  421. _sureButton = [UIButton buttonWithType:UIButtonTypeCustom];
  422. [_sureButton setTitle:kLocalizedString(@"确认") forState:UIControlStateNormal];
  423. [_sureButton setTitleColor:Color_Blue forState:UIControlStateNormal];
  424. _sureButton.titleLabel.font = Font_B(16);
  425. }
  426. return _sureButton;
  427. }
  428. - (UILabel *)titleLabel {
  429. if (_titleLabel == nil) {
  430. _titleLabel = [UILabel createLabelTextColor:Color_TextFont fount:Font(16)];
  431. _titleLabel.text = kLocalizedString(@"地区选择");
  432. _titleLabel.textAlignment = NSTextAlignmentCenter;
  433. }
  434. return _titleLabel;
  435. }
  436. - (UIPickerView *)pickerView {
  437. if (_pickerView == nil) {
  438. _pickerView = [[UIPickerView alloc] init];
  439. _pickerView.delegate = self;
  440. _pickerView.dataSource = self;
  441. }
  442. return _pickerView;
  443. }
  444. @end