No Description

FLAddressPickerView.m 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. //
  2. // FLAddressPickerView.m
  3. // FirstLink
  4. //
  5. // Created by unicode on 14-11-4.
  6. // Copyright (c) 2014年 FirstLink. All rights reserved.
  7. //
  8. #import "FLAddressPickerView.h"
  9. #import "FKAddressDetailViewModel.h"
  10. #import <POP.h>
  11. @interface FLAddressPickerView ()
  12. <UIPickerViewDataSource, UIPickerViewDelegate>
  13. @property (nonatomic, strong) UIPickerView *pickerView;
  14. @property (nonatomic, strong) UIView *bannerView;
  15. @property (nonatomic, strong) UIButton *doneButton;
  16. @property (nonatomic, strong) UIButton *cancelButton;
  17. /**
  18. * 数据格式为省数组、市数组、区数组
  19. */
  20. @property (nonatomic, strong) NSMutableArray *provinceCityDataSource;
  21. @end
  22. @implementation FLAddressPickerView
  23. /*
  24. // Only override drawRect: if you perform custom drawing.
  25. // An empty implementation adversely affects performance during animation.
  26. - (void)drawRect:(CGRect)rect {
  27. // Drawing code
  28. }
  29. */
  30. - (id)initWithFrame:(CGRect)frame {
  31. self = [super initWithFrame:frame];
  32. if (self) {
  33. self.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.15];
  34. [self layoutAllSubviews];
  35. }
  36. return self;
  37. }
  38. #pragma mark - Done and Cancel Action
  39. - (IBAction)cancel:(id)sender {
  40. POPSpringAnimation *springAnimation = [POPSpringAnimation animationWithPropertyNamed:kPOPLayerPosition];
  41. springAnimation.springBounciness = 2;
  42. springAnimation.springSpeed = 20;
  43. springAnimation.toValue = [NSValue valueWithCGPoint:CGPointMake(UISCREENWIDTH/2, self.frame.size.height*3/2)];
  44. [self pop_addAnimation:springAnimation forKey:@"hideaddresspicker"];
  45. [self removeFromSuperview];
  46. }
  47. - (IBAction)done:(id)sender {
  48. if (_doneAction) {
  49. NSString *province = [self titleOfProvinceAtIndex:[self.pickerView selectedRowInComponent:0]];
  50. NSString *city = [self titleOfCityAtIndex:[self.pickerView selectedRowInComponent:0]
  51. cityIndex:[self.pickerView selectedRowInComponent:1]];
  52. NSString *district = [self titleOfDistrictAtIndex:[self.pickerView selectedRowInComponent:0]
  53. cityIndex:[self.pickerView selectedRowInComponent:1]
  54. districtIndex:[self.pickerView selectedRowInComponent:2]];
  55. _doneAction([FLStringHelper replaceNilWithEmpty:province],
  56. [FLStringHelper replaceNilWithEmpty:city],
  57. [FLStringHelper replaceNilWithEmpty:district]);
  58. }
  59. [self cancel:nil];
  60. }
  61. #pragma mark - Method
  62. - (NSInteger)numberOfProvince {
  63. return self.provinceCityDataSource.count;
  64. }
  65. - (NSInteger)numberOfCityAtProvinceIndex:(NSInteger)provinceIndex {
  66. if (provinceIndex < self.provinceCityDataSource.count) {
  67. NSDictionary *provinceDict = self.provinceCityDataSource[provinceIndex];
  68. NSArray *cities = [provinceDict valueForKey:@"lists"];
  69. if ([cities isKindOfClass:[NSArray class]]) {
  70. return cities.count;
  71. }
  72. }
  73. return 0;
  74. }
  75. - (NSInteger)numberOfDistrictAtProvinceIndex:(NSInteger)provinceIndex cityIndex:(NSInteger)cityIndex {
  76. if (provinceIndex < self.provinceCityDataSource.count) {
  77. NSDictionary *provinceDict = self.provinceCityDataSource[provinceIndex];
  78. NSArray *cities = [provinceDict valueForKey:@"lists"];
  79. if ([cities isKindOfClass:[NSArray class]] && cityIndex < cities.count) {
  80. NSDictionary *cityDict = cities[cityIndex];
  81. NSArray *districts = [cityDict valueForKey:@"lists"];
  82. if ([districts isKindOfClass:[NSArray class]]) {
  83. return districts.count;
  84. }
  85. }
  86. }
  87. return 0;
  88. }
  89. - (NSString *)titleOfProvinceAtIndex:(NSInteger)provinceIndex {
  90. if (provinceIndex < self.provinceCityDataSource.count) {
  91. NSDictionary *provinceDict = self.provinceCityDataSource[provinceIndex];
  92. return [FLStringHelper replaceNilWithEmpty:[provinceDict valueForKey:@"name"]];
  93. }
  94. return @"";
  95. }
  96. - (NSString *)titleOfCityAtIndex:(NSInteger)provinceIndex cityIndex:(NSInteger)cityIndex {
  97. if (provinceIndex < self.provinceCityDataSource.count) {
  98. NSDictionary *provinceDict = self.provinceCityDataSource[provinceIndex];
  99. NSArray *cities = [provinceDict valueForKey:@"lists"];
  100. if (cityIndex < cities.count) {
  101. NSDictionary *cityDict = cities[cityIndex];
  102. return [FLStringHelper replaceNilWithEmpty:[cityDict valueForKey:@"name"]];
  103. }
  104. }
  105. return @"";
  106. }
  107. - (NSString *)titleOfDistrictAtIndex:(NSInteger)provinceIndex cityIndex:(NSInteger)cityIndex districtIndex:(NSInteger)districtIndex {
  108. if (provinceIndex < self.provinceCityDataSource.count) {
  109. NSDictionary *provinceDict = self.provinceCityDataSource[provinceIndex];
  110. NSArray *cities = [provinceDict valueForKey:@"lists"];
  111. if (cityIndex < cities.count) {
  112. NSDictionary *cityDict = cities[cityIndex];
  113. NSArray *districts = [cityDict valueForKey:@"lists"];
  114. if (districtIndex < districts.count) {
  115. NSDictionary *districtDict = districts[districtIndex];
  116. return [FLStringHelper replaceNilWithEmpty:[districtDict valueForKey:@"name"]];
  117. }
  118. }
  119. }
  120. return @"";
  121. }
  122. #pragma mark - Picker View Delegate
  123. -(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
  124. return 3;
  125. }
  126. -(NSInteger) pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
  127. switch (component) {
  128. case 0:
  129. return [self numberOfProvince];
  130. break;
  131. case 1:
  132. return [self numberOfCityAtProvinceIndex:[self.pickerView selectedRowInComponent:0]];
  133. break;
  134. case 2:
  135. return [self numberOfDistrictAtProvinceIndex:[self.pickerView selectedRowInComponent:0]
  136. cityIndex:[self.pickerView selectedRowInComponent:1]];
  137. break;
  138. default:
  139. break;
  140. }
  141. return 0;
  142. }
  143. - (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
  144. switch (component) {
  145. case 0:
  146. return [self titleOfProvinceAtIndex:row];
  147. break;
  148. case 1:
  149. return [self titleOfCityAtIndex:[self.pickerView selectedRowInComponent:0]
  150. cityIndex:row];
  151. break;
  152. case 2:
  153. return [self titleOfDistrictAtIndex:[self.pickerView selectedRowInComponent:0]
  154. cityIndex:[self.pickerView selectedRowInComponent:1]
  155. districtIndex:row];
  156. default:
  157. break;
  158. }
  159. return @"";
  160. }
  161. - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
  162. /**
  163. * 选择省和市时重新加载UIPickerView
  164. */
  165. if (component != 2) {
  166. [pickerView reloadAllComponents];
  167. }
  168. }
  169. #pragma mark - Layout
  170. - (void)layoutAllSubviews {
  171. WeakSelf(weakSelf);
  172. [self addSubview:self.pickerView];
  173. if (IS_IPHONE_X) {
  174. [self.pickerView mas_remakeConstraints:^(MASConstraintMaker *make) {
  175. make.left.right.equalTo(weakSelf);
  176. make.bottom.equalTo(weakSelf).offset(-34);
  177. make.height.mas_equalTo(216);
  178. }];
  179. UIView *bottomView = [UIView new];
  180. bottomView.backgroundColor = [UIColor whiteColor];
  181. [self addSubview:bottomView];
  182. [bottomView mas_remakeConstraints:^(MASConstraintMaker *make) {
  183. make.left.right.bottom.equalTo(weakSelf);
  184. make.top.equalTo(weakSelf.pickerView.mas_bottom);
  185. }];
  186. } else {
  187. [self.pickerView mas_remakeConstraints:^(MASConstraintMaker *make) {
  188. make.left.right.bottom.equalTo(weakSelf);
  189. make.height.mas_equalTo(216);
  190. }];
  191. }
  192. [self addSubview:self.bannerView];
  193. [self.bannerView mas_remakeConstraints:^(MASConstraintMaker *make) {
  194. make.left.right.equalTo(weakSelf.pickerView);
  195. make.bottom.equalTo(weakSelf.pickerView.mas_top);
  196. make.height.mas_equalTo(44);
  197. }];
  198. [self addSubview:self.doneButton];
  199. [self.doneButton mas_remakeConstraints:^(MASConstraintMaker *make) {
  200. make.left.equalTo(weakSelf.mas_right).offset(-75);
  201. make.top.equalTo(weakSelf.pickerView.mas_top).offset(-44);
  202. make.size.mas_equalTo(CGSizeMake(60, 44));
  203. }];
  204. [self addSubview:self.cancelButton];
  205. [self.cancelButton mas_remakeConstraints:^(MASConstraintMaker *make) {
  206. make.left.equalTo(weakSelf).offset(16);
  207. make.top.equalTo(weakSelf.pickerView.mas_top).offset(-44);
  208. make.size.mas_equalTo(CGSizeMake(60, 44));
  209. }];
  210. }
  211. #pragma mark - Property
  212. - (UIPickerView *)pickerView {
  213. if (!_pickerView) {
  214. _pickerView = [[UIPickerView alloc] init];
  215. _pickerView.backgroundColor = [UIColor whiteColor];
  216. _pickerView.delegate = self;
  217. _pickerView.dataSource = self;
  218. }
  219. return _pickerView;
  220. }
  221. - (UIView*)bannerView {
  222. if (!_bannerView) {
  223. _bannerView = [[UIView alloc] init];
  224. _bannerView.backgroundColor = [UIColor colorWithRed:235.0/255.0
  225. green:236.0/255.0
  226. blue:239.0/255.0
  227. alpha:1.0];
  228. }
  229. return _bannerView;
  230. }
  231. - (UIButton *)doneButton {
  232. if (!_doneButton) {
  233. _doneButton = [UIButton buttonWithType:UIButtonTypeCustom];
  234. [_doneButton setTitle:@"完成" forState:UIControlStateNormal];
  235. [_doneButton addTarget:self
  236. action:@selector(done:)
  237. forControlEvents:UIControlEventTouchUpInside];
  238. [_doneButton setTitleColor:BLUETEXTCOLOR forState:UIControlStateNormal];
  239. }
  240. return _doneButton;
  241. }
  242. - (UIButton *)cancelButton {
  243. if (!_cancelButton) {
  244. _cancelButton = [UIButton buttonWithType:UIButtonTypeCustom];
  245. [_cancelButton setTitle:@"取消" forState:UIControlStateNormal];
  246. [_cancelButton addTarget:self
  247. action:@selector(cancel:)
  248. forControlEvents:UIControlEventTouchUpInside];
  249. [_cancelButton setTitleColor:BLUETEXTCOLOR forState:UIControlStateNormal];
  250. }
  251. return _cancelButton;
  252. }
  253. - (NSMutableArray *)provinceCityDataSource {
  254. if (!_provinceCityDataSource) {
  255. NSString *jsonFilePath = [FKAddressDetailViewModel latestVersionProvinceCityFilePath];
  256. NSError *error;
  257. NSDictionary *json;
  258. NSData *data = [NSData dataWithContentsOfFile:jsonFilePath];
  259. json = [NSJSONSerialization JSONObjectWithData:data
  260. options:kNilOptions
  261. error:&error];
  262. if (!error) {
  263. _provinceCityDataSource = [[json valueForKey:@"data"] valueForKey:@"lists"];
  264. }
  265. }
  266. return _provinceCityDataSource;
  267. }
  268. @end