123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310 |
- //
- // FLAddressPickerView.m
- // FirstLink
- //
- // Created by unicode on 14-11-4.
- // Copyright (c) 2014年 FirstLink. All rights reserved.
- //
- #import "FLAddressPickerView.h"
- #import "FKAddressDetailViewModel.h"
- #import <POP.h>
- @interface FLAddressPickerView ()
- <UIPickerViewDataSource, UIPickerViewDelegate>
- @property (nonatomic, strong) UIPickerView *pickerView;
- @property (nonatomic, strong) UIView *bannerView;
- @property (nonatomic, strong) UIButton *doneButton;
- @property (nonatomic, strong) UIButton *cancelButton;
- /**
- * 数据格式为省数组、市数组、区数组
- */
- @property (nonatomic, strong) NSMutableArray *provinceCityDataSource;
- @end
- @implementation FLAddressPickerView
- /*
- // Only override drawRect: if you perform custom drawing.
- // An empty implementation adversely affects performance during animation.
- - (void)drawRect:(CGRect)rect {
- // Drawing code
- }
- */
- - (id)initWithFrame:(CGRect)frame {
- self = [super initWithFrame:frame];
- if (self) {
- self.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.15];
-
- [self layoutAllSubviews];
- }
- return self;
- }
- #pragma mark - Done and Cancel Action
- - (IBAction)cancel:(id)sender {
- POPSpringAnimation *springAnimation = [POPSpringAnimation animationWithPropertyNamed:kPOPLayerPosition];
- springAnimation.springBounciness = 2;
- springAnimation.springSpeed = 20;
- springAnimation.toValue = [NSValue valueWithCGPoint:CGPointMake(UISCREENWIDTH/2, self.frame.size.height*3/2)];
- [self pop_addAnimation:springAnimation forKey:@"hideaddresspicker"];
- [self removeFromSuperview];
- }
- - (IBAction)done:(id)sender {
- if (_doneAction) {
- NSString *province = [self titleOfProvinceAtIndex:[self.pickerView selectedRowInComponent:0]];
- NSString *city = [self titleOfCityAtIndex:[self.pickerView selectedRowInComponent:0]
- cityIndex:[self.pickerView selectedRowInComponent:1]];
- NSString *district = [self titleOfDistrictAtIndex:[self.pickerView selectedRowInComponent:0]
- cityIndex:[self.pickerView selectedRowInComponent:1]
- districtIndex:[self.pickerView selectedRowInComponent:2]];
- _doneAction([FLStringHelper replaceNilWithEmpty:province],
- [FLStringHelper replaceNilWithEmpty:city],
- [FLStringHelper replaceNilWithEmpty:district]);
- }
-
- [self cancel:nil];
- }
- #pragma mark - Method
- - (NSInteger)numberOfProvince {
- return self.provinceCityDataSource.count;
- }
- - (NSInteger)numberOfCityAtProvinceIndex:(NSInteger)provinceIndex {
- if (provinceIndex < self.provinceCityDataSource.count) {
- NSDictionary *provinceDict = self.provinceCityDataSource[provinceIndex];
- NSArray *cities = [provinceDict valueForKey:@"lists"];
- if ([cities isKindOfClass:[NSArray class]]) {
- return cities.count;
- }
- }
- return 0;
- }
- - (NSInteger)numberOfDistrictAtProvinceIndex:(NSInteger)provinceIndex cityIndex:(NSInteger)cityIndex {
- if (provinceIndex < self.provinceCityDataSource.count) {
- NSDictionary *provinceDict = self.provinceCityDataSource[provinceIndex];
-
- NSArray *cities = [provinceDict valueForKey:@"lists"];
- if ([cities isKindOfClass:[NSArray class]] && cityIndex < cities.count) {
-
- NSDictionary *cityDict = cities[cityIndex];
- NSArray *districts = [cityDict valueForKey:@"lists"];
- if ([districts isKindOfClass:[NSArray class]]) {
- return districts.count;
- }
- }
- }
- return 0;
- }
- - (NSString *)titleOfProvinceAtIndex:(NSInteger)provinceIndex {
- if (provinceIndex < self.provinceCityDataSource.count) {
- NSDictionary *provinceDict = self.provinceCityDataSource[provinceIndex];
- return [FLStringHelper replaceNilWithEmpty:[provinceDict valueForKey:@"name"]];
- }
- return @"";
- }
- - (NSString *)titleOfCityAtIndex:(NSInteger)provinceIndex cityIndex:(NSInteger)cityIndex {
- if (provinceIndex < self.provinceCityDataSource.count) {
- NSDictionary *provinceDict = self.provinceCityDataSource[provinceIndex];
- NSArray *cities = [provinceDict valueForKey:@"lists"];
- if (cityIndex < cities.count) {
- NSDictionary *cityDict = cities[cityIndex];
- return [FLStringHelper replaceNilWithEmpty:[cityDict valueForKey:@"name"]];
- }
- }
- return @"";
- }
- - (NSString *)titleOfDistrictAtIndex:(NSInteger)provinceIndex cityIndex:(NSInteger)cityIndex districtIndex:(NSInteger)districtIndex {
- if (provinceIndex < self.provinceCityDataSource.count) {
- NSDictionary *provinceDict = self.provinceCityDataSource[provinceIndex];
- NSArray *cities = [provinceDict valueForKey:@"lists"];
- if (cityIndex < cities.count) {
- NSDictionary *cityDict = cities[cityIndex];
- NSArray *districts = [cityDict valueForKey:@"lists"];
- if (districtIndex < districts.count) {
- NSDictionary *districtDict = districts[districtIndex];
- return [FLStringHelper replaceNilWithEmpty:[districtDict valueForKey:@"name"]];
- }
- }
- }
- return @"";
- }
- #pragma mark - Picker View Delegate
- -(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
- return 3;
- }
- -(NSInteger) pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
- switch (component) {
- case 0:
- return [self numberOfProvince];
- break;
- case 1:
- return [self numberOfCityAtProvinceIndex:[self.pickerView selectedRowInComponent:0]];
- break;
- case 2:
- return [self numberOfDistrictAtProvinceIndex:[self.pickerView selectedRowInComponent:0]
- cityIndex:[self.pickerView selectedRowInComponent:1]];
- break;
- default:
- break;
- }
- return 0;
- }
- - (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
- switch (component) {
- case 0:
- return [self titleOfProvinceAtIndex:row];
- break;
- case 1:
- return [self titleOfCityAtIndex:[self.pickerView selectedRowInComponent:0]
- cityIndex:row];
- break;
- case 2:
- return [self titleOfDistrictAtIndex:[self.pickerView selectedRowInComponent:0]
- cityIndex:[self.pickerView selectedRowInComponent:1]
- districtIndex:row];
- default:
- break;
- }
- return @"";
- }
- - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
- /**
- * 选择省和市时重新加载UIPickerView
- */
- if (component != 2) {
- [pickerView reloadAllComponents];
- }
- }
- #pragma mark - Layout
- - (void)layoutAllSubviews {
- WeakSelf(weakSelf);
- [self addSubview:self.pickerView];
- if (IS_IPHONE_X) {
- [self.pickerView mas_remakeConstraints:^(MASConstraintMaker *make) {
- make.left.right.equalTo(weakSelf);
- make.bottom.equalTo(weakSelf).offset(-34);
- make.height.mas_equalTo(216);
- }];
-
- UIView *bottomView = [UIView new];
- bottomView.backgroundColor = [UIColor whiteColor];
- [self addSubview:bottomView];
- [bottomView mas_remakeConstraints:^(MASConstraintMaker *make) {
- make.left.right.bottom.equalTo(weakSelf);
- make.top.equalTo(weakSelf.pickerView.mas_bottom);
- }];
- } else {
- [self.pickerView mas_remakeConstraints:^(MASConstraintMaker *make) {
- make.left.right.bottom.equalTo(weakSelf);
- make.height.mas_equalTo(216);
- }];
- }
-
- [self addSubview:self.bannerView];
- [self.bannerView mas_remakeConstraints:^(MASConstraintMaker *make) {
- make.left.right.equalTo(weakSelf.pickerView);
- make.bottom.equalTo(weakSelf.pickerView.mas_top);
- make.height.mas_equalTo(44);
- }];
-
- [self addSubview:self.doneButton];
- [self.doneButton mas_remakeConstraints:^(MASConstraintMaker *make) {
- make.left.equalTo(weakSelf.mas_right).offset(-75);
- make.top.equalTo(weakSelf.pickerView.mas_top).offset(-44);
- make.size.mas_equalTo(CGSizeMake(60, 44));
- }];
-
- [self addSubview:self.cancelButton];
- [self.cancelButton mas_remakeConstraints:^(MASConstraintMaker *make) {
- make.left.equalTo(weakSelf).offset(16);
- make.top.equalTo(weakSelf.pickerView.mas_top).offset(-44);
- make.size.mas_equalTo(CGSizeMake(60, 44));
- }];
- }
- #pragma mark - Property
- - (UIPickerView *)pickerView {
- if (!_pickerView) {
- _pickerView = [[UIPickerView alloc] init];
- _pickerView.backgroundColor = [UIColor whiteColor];
- _pickerView.delegate = self;
- _pickerView.dataSource = self;
- }
- return _pickerView;
- }
- - (UIView*)bannerView {
- if (!_bannerView) {
- _bannerView = [[UIView alloc] init];
- _bannerView.backgroundColor = [UIColor colorWithRed:235.0/255.0
- green:236.0/255.0
- blue:239.0/255.0
- alpha:1.0];
- }
- return _bannerView;
- }
- - (UIButton *)doneButton {
- if (!_doneButton) {
- _doneButton = [UIButton buttonWithType:UIButtonTypeCustom];
- [_doneButton setTitle:@"完成" forState:UIControlStateNormal];
- [_doneButton addTarget:self
- action:@selector(done:)
- forControlEvents:UIControlEventTouchUpInside];
- [_doneButton setTitleColor:BLUETEXTCOLOR forState:UIControlStateNormal];
- }
- return _doneButton;
- }
- - (UIButton *)cancelButton {
- if (!_cancelButton) {
- _cancelButton = [UIButton buttonWithType:UIButtonTypeCustom];
- [_cancelButton setTitle:@"取消" forState:UIControlStateNormal];
- [_cancelButton addTarget:self
- action:@selector(cancel:)
- forControlEvents:UIControlEventTouchUpInside];
- [_cancelButton setTitleColor:BLUETEXTCOLOR forState:UIControlStateNormal];
- }
- return _cancelButton;
- }
- - (NSMutableArray *)provinceCityDataSource {
- if (!_provinceCityDataSource) {
- NSString *jsonFilePath = [FKAddressDetailViewModel latestVersionProvinceCityFilePath];
-
- NSError *error;
- NSDictionary *json;
- NSData *data = [NSData dataWithContentsOfFile:jsonFilePath];
- json = [NSJSONSerialization JSONObjectWithData:data
- options:kNilOptions
- error:&error];
- if (!error) {
- _provinceCityDataSource = [[json valueForKey:@"data"] valueForKey:@"lists"];
- }
- }
- return _provinceCityDataSource;
- }
- @end
|