YLCityPickerView.m 16 KB

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