//
|
// ZJPickerView.m
|
// ZJPickerView <https://github.com/Abnerzj/ZJPickerView>
|
//
|
// Created by Abnerzj on 2018/1/12.
|
// Copyright © 2017年 Abnerzj. All rights reserved.
|
//
|
// This source code is licensed under the MIT-style license found in the
|
// LICENSE file in the root directory of this source tree.
|
//
|
|
#import "ZJPickerView.h"
|
#import "HDLUtlisXM.h"
|
|
// content: NSString type
|
NSString * const ZJPickerViewPropertyCanceBtnTitleKey = @"ZJPickerViewPropertyCanceBtnTitleKey";
|
NSString * const ZJPickerViewPropertySureBtnTitleKey = @"ZJPickerViewPropertySureBtnTitleKey";
|
NSString * const ZJPickerViewPropertyTipLabelTextKey = @"ZJPickerViewPropertyTipLabelTextKey";
|
NSString * const ZJPickerViewPropertyDividedSymbolKey = @"ZJPickerViewPropertyDividedSymbolKey";
|
|
// color: UIColor type
|
NSString * const ZJPickerViewPropertyCanceBtnTitleColorKey = @"ZJPickerViewPropertyCanceBtnTitleColorKey";
|
NSString * const ZJPickerViewPropertySureBtnTitleColorKey = @"ZJPickerViewPropertySureBtnTitleColorKey";
|
NSString * const ZJPickerViewPropertyTipLabelTextColorKey = @"ZJPickerViewPropertyTipLabelTextColorKey";
|
NSString * const ZJPickerViewPropertyLineViewBackgroundColorKey = @"ZJPickerViewPropertyLineViewBackgroundColorKey";
|
|
// font: UIFont type
|
NSString * const ZJPickerViewPropertyCanceBtnTitleFontKey = @"ZJPickerViewPropertyCanceBtnTitleFontKey";
|
NSString * const ZJPickerViewPropertySureBtnTitleFontKey = @"ZJPickerViewPropertySureBtnTitleFontKey";
|
NSString * const ZJPickerViewPropertyTipLabelTextFontKey = @"ZJPickerViewPropertyTipLabelTextFontKey";
|
|
// pickerView:
|
// CGFloat type
|
NSString * const ZJPickerViewPropertyPickerViewHeightKey = @"ZJPickerViewPropertyPickerViewHeightKey";
|
NSString * const ZJPickerViewPropertyOneComponentRowHeightKey = @"ZJPickerViewPropertyOneComponentRowHeightKey";
|
// NSDictionary type
|
NSString * const ZJPickerViewPropertySelectRowTitleAttrKey = @"ZJPickerViewPropertySelectRowTitleAttrKey";
|
NSString * const ZJPickerViewPropertyUnSelectRowTitleAttrKey = @"ZJPickerViewPropertyUnSelectRowTitleAttrKey";
|
// UIColor type
|
NSString * const ZJPickerViewPropertySelectRowLineBackgroundColorKey = @"ZJPickerViewPropertySelectRowLineBackgroundColorKey";
|
|
// other:
|
// BOOL type
|
NSString * const ZJPickerViewPropertyIsTouchBackgroundHideKey = @"ZJPickerViewPropertyIsTouchBackgroundHideKey";
|
NSString * const ZJPickerViewPropertyIsShowTipLabelKey = @"ZJPickerViewPropertyIsShowTipLabelKey";
|
NSString * const ZJPickerViewPropertyIsShowSelectContentKey = @"ZJPickerViewPropertyIsShowSelectContentKey";
|
NSString * const ZJPickerViewPropertyIsScrollToSelectedRowKey = @"ZJPickerViewPropertyIsScrollToSelectedRowKey";
|
NSString * const ZJPickerViewPropertyIsDividedSelectContentKey = @"ZJPickerViewPropertyIsDividedSelectContentKey";
|
NSString * const ZJPickerViewPropertyIsAnimationShowKey = @"ZJPickerViewPropertyIsAnimationShowKey";
|
// CGFloat type
|
NSString * const ZJPickerViewPropertyBackgroundAlphaKey = @"ZJPickerViewPropertyBackgroundAlphaKey";
|
|
//2019-09-05
|
NSString * const ZJPickerViewPropertyDefaultSelectedIndexKey = @"ZJPickerViewPropertyDefaultSelectedIndexKey";
|
|
|
static const CGFloat toolViewHeight = 44.0f; // tool view height
|
static const CGFloat canceBtnWidth = 68.0f; // cance button or sure button height
|
static NSString * const kDividedSymbol = @","; // divided symbol
|
|
@interface ZJPickerView ()<UIPickerViewDataSource, UIPickerViewDelegate>
|
|
// property
|
@property (nonatomic, strong) NSMutableArray *dataList; // data list
|
@property (nonatomic, strong) NSMutableArray *mSecondDataList; // 二级 list
|
@property (nonatomic, strong) NSMutableArray *mThirdDataList; // 三级 list
|
|
|
@property (nonatomic, strong) NSMutableDictionary *propertyDict; // property dictionary
|
|
// pickerView
|
@property (nonatomic, assign) NSUInteger component; // component numbers, default 0
|
@property (nonatomic, assign) CGFloat pickerViewHeight; // pickerView height, default 224 pt
|
@property (nonatomic, assign) CGFloat oneComponentRowHeight; // one component row height, default 32 pt
|
@property (nonatomic, strong) NSDictionary *selectRowTitleAttribute; // select row titlt attribute
|
@property (nonatomic, strong) NSDictionary *unSelectRowTitleAttribute; // unSelect row titlt attribute
|
@property (nonatomic, strong) UIColor *selectRowLineBackgroundColor; // select row top and bottom line backgroundColor
|
@property (nonatomic, copy) NSString *dividedSymbol; // divided symbol, default commas
|
|
@property (nonatomic, assign) BOOL isTouchBackgroundHide; // touch background is hide, default NO
|
@property (nonatomic, assign) BOOL isShowTipLabel; // is show tipLabel, default NO.
|
@property (nonatomic, assign) BOOL isShowSelectContent; // scroll component is update and show select content in tipLabel, default NO
|
@property (nonatomic, assign) BOOL isScrollToSelectedRow; // pickerView will show scroll to selected row, default NO
|
@property (nonatomic, assign) BOOL isDividedSelectContent; // select content is divided, default NO
|
@property (nonatomic, assign) BOOL isAnimationShow; // show pickerView is need Animation, default YES
|
@property (nonatomic, assign) BOOL isSettedSelectRowLineBackgroundColor; // is setted select row top and bottom line backgroundColor, default NO
|
@property (nonatomic, assign) CGFloat backgroundAlpha; // background alpha, default 0.5(0.0~1.0)
|
//@property (nonatomic, copy) void(^completion)(NSString * _Nullable selectContent); // select content
|
@property (nonatomic, copy) void(^completion)(NSInteger, NSInteger, NSInteger); //2019-09-05 select content
|
|
// subviews
|
@property (nonatomic, strong) UIView *backgroundView;
|
@property (nonatomic, strong) UIView *contentView;
|
@property (nonatomic, strong) UIPickerView *pickerView;
|
@property (nonatomic, strong) UIButton *canceBtn;
|
@property (nonatomic, strong) UIButton *sureBtn;
|
@property (nonatomic, strong) UILabel *tipLabel;
|
@property (nonatomic, strong) UIView *lineView;
|
|
//2019-9-5 新增默认选择项,和回调选中的索引
|
@property (nonatomic, assign) NSInteger selectIndex1;
|
@property (nonatomic, assign) NSInteger selectIndex2;
|
@property (nonatomic, assign) NSInteger selectIndex3;
|
@property (nonatomic, strong) UIColor *btnColor;
|
|
@property (nonatomic, assign) BOOL isLinked; //是否不联动
|
|
@end
|
|
@implementation ZJPickerView
|
|
+ (ZJPickerView *)sharedView {
|
static dispatch_once_t once;
|
static ZJPickerView *sharedView;
|
dispatch_once(&once, ^{ sharedView = [[self alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; });
|
return sharedView;
|
}
|
|
#pragma mark - Instance Methods
|
- (instancetype)initWithFrame:(CGRect)frame {
|
if((self = [super initWithFrame:frame])) {
|
[self initDefaultConfig];
|
self.dataList = [NSMutableArray array];
|
self.mSecondDataList = [NSMutableArray array];
|
self.mThirdDataList = [NSMutableArray array];
|
self.propertyDict = [NSMutableDictionary dictionary];
|
[self initSubViews];
|
}
|
return self;
|
}
|
|
- (void)initDefaultConfig
|
{
|
|
self.isLinked = YES;
|
self.selectIndex1 = 0;
|
self.selectIndex2 = 0;
|
self.selectIndex3 = 0;
|
self.component = 0;
|
self.pickerViewHeight = 224.0f;
|
self.oneComponentRowHeight = 32.0f;
|
self.selectRowTitleAttribute = @{NSForegroundColorAttributeName : [UIColor blackColor], NSFontAttributeName : [UIFont systemFontOfSize:20.0f]};
|
self.unSelectRowTitleAttribute = @{NSForegroundColorAttributeName : [UIColor lightGrayColor], NSFontAttributeName : [UIFont systemFontOfSize:20.0f]};
|
self.selectRowLineBackgroundColor = [UIColor colorWithRed:222.0/255.0 green:222.0/255.0 blue:222.0/255.0 alpha:1.0];
|
self.btnColor = [UIColor colorWithRed:255.0/255.0 green:87.0/255.0 blue:34.0/255.0 alpha:1.0];
|
self.dividedSymbol = kDividedSymbol;
|
|
self.isTouchBackgroundHide = NO;
|
self.isShowTipLabel = NO;
|
self.isShowSelectContent = NO;
|
self.isScrollToSelectedRow = NO;
|
self.isDividedSelectContent = NO;
|
self.isAnimationShow = YES;
|
self.isSettedSelectRowLineBackgroundColor = NO;
|
self.backgroundAlpha = 0.5f;
|
}
|
|
-(void)setRadiusWithView:(UIView *)mView{
|
//顶部圆角
|
UIRectCorner corners = UIRectCornerTopLeft | UIRectCornerTopRight;
|
if (@available(iOS 11.0, *)) {
|
mView.layer.cornerRadius = 20;
|
mView.layer.maskedCorners = (CACornerMask)corners; // 左上圆角
|
}else{
|
UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:mView.bounds byRoundingCorners:corners cornerRadii:CGSizeMake(20,20)];
|
//创建 layer
|
CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
|
maskLayer.frame = mView.bounds;
|
//赋值
|
maskLayer.path = maskPath.CGPath;
|
mView.layer.mask = maskLayer;
|
}
|
}
|
|
|
- (void)initSubViews
|
{
|
// background view
|
UIView *backgroundView = [[UIView alloc] initWithFrame:self.bounds];
|
backgroundView.backgroundColor = [UIColor blackColor];
|
backgroundView.alpha = self.backgroundAlpha;
|
[self addSubview:backgroundView];
|
|
// add tap Gesture
|
UITapGestureRecognizer *tapbgGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(touchBackgroundView)];
|
[backgroundView addGestureRecognizer:tapbgGesture];
|
|
// content view
|
UIView *contentView = [[UIView alloc] initWithFrame:CGRectMake(0, self.frame.size.height - toolViewHeight - self.pickerViewHeight, self.frame.size.width, toolViewHeight + self.pickerViewHeight)];
|
contentView.backgroundColor = [UIColor whiteColor];
|
[self setRadiusWithView:contentView];
|
[self addSubview:contentView];
|
|
// tool view
|
UIView *toolView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, toolViewHeight)];
|
toolView.backgroundColor = [UIColor whiteColor];
|
[self setRadiusWithView:toolView];
|
[contentView addSubview:toolView];
|
|
// UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, 1)];
|
// lineView.backgroundColor = [UIColor lightGrayColor];
|
// [contentView addSubview:lineView];
|
//
|
// cance button
|
NSArray *diffLanguageTitles = [self getDiffLanguageCanceAndSureBtnTitles];
|
UIButton *canceBtn = [UIButton buttonWithType:UIButtonTypeCustom];
|
canceBtn.frame = CGRectMake(0, 0, canceBtnWidth, toolView.frame.size.height);
|
[canceBtn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
|
[canceBtn setTitle:diffLanguageTitles.firstObject forState:UIControlStateNormal];
|
// [canceBtn.titleLabel setFont:[UIFont systemFontOfSize:17.0]];
|
canceBtn.titleLabel.font = [UIFont fontWithName:APP_UIFont size:14];
|
[canceBtn setTag:0];
|
[canceBtn addTarget:self action:@selector(userAction:) forControlEvents:UIControlEventTouchUpInside];
|
[toolView addSubview:canceBtn];
|
|
// sure button
|
UIButton *sureBtn = [UIButton buttonWithType:UIButtonTypeCustom];
|
sureBtn.frame = CGRectMake(toolView.frame.size.width - canceBtnWidth, 0, canceBtnWidth, toolView.frame.size.height);
|
[sureBtn setTitleColor:self.btnColor forState:UIControlStateNormal];
|
[sureBtn setTitle:diffLanguageTitles.lastObject forState:UIControlStateNormal];
|
// [sureBtn.titleLabel setFont:[UIFont systemFontOfSize:17.0]];
|
sureBtn.titleLabel.font = [UIFont fontWithName:APP_UIFont size:14];
|
[sureBtn setTag:1];
|
[sureBtn addTarget:self action:@selector(userAction:) forControlEvents:UIControlEventTouchUpInside];
|
[toolView addSubview:sureBtn];
|
|
// center title
|
UILabel *tipLabel = [[UILabel alloc] initWithFrame:CGRectMake(canceBtn.frame.size.width, 0, toolView.frame.size.width - canceBtn.frame.size.width*2, toolView.frame.size.height)];
|
tipLabel.text = @"";
|
tipLabel.textColor = [UIColor darkTextColor];
|
// tipLabel.font = [UIFont systemFontOfSize:17.0];
|
tipLabel.font = [UIFont fontWithName:APP_UIFont size:16];
|
tipLabel.textAlignment = NSTextAlignmentCenter;
|
tipLabel.hidden = !self.isShowTipLabel;
|
[toolView addSubview:tipLabel];
|
|
// line view
|
UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0, toolView.frame.size.height - 0.5f, self.frame.size.width, 0.5f)];
|
lineView.backgroundColor = [UIColor colorWithRed:222.0/255.0 green:222.0/255.0 blue:222.0/255.0 alpha:1.0];
|
[toolView addSubview:lineView];
|
|
// pickerView
|
UIPickerView *pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, toolView.frame.size.height, self.frame.size.width, self.pickerViewHeight)];
|
pickerView.dataSource = self;
|
pickerView.delegate = self;
|
[contentView addSubview:pickerView];
|
|
// global variable
|
self.backgroundView = backgroundView;
|
self.contentView = contentView;
|
self.canceBtn = canceBtn;
|
self.sureBtn = sureBtn;
|
self.lineView = lineView;
|
self.tipLabel = tipLabel;
|
self.pickerView = pickerView;
|
}
|
|
#pragma mark - UIPickerView DataSource & Delegate
|
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
|
{
|
// if(self.isLinked){
|
// return self.component;
|
// }else{
|
// return 2;
|
// }
|
return self.component;
|
}
|
|
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
|
{
|
if(self.isLinked){
|
return [self getDataWithComponent:component].count;
|
}else{
|
return [self getDataWithComponentNoLinked:component].count;
|
}
|
}
|
|
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
|
{
|
NSArray *componentArray;
|
if(self.isLinked){
|
componentArray = [self getDataWithComponent:component];
|
}else{
|
componentArray = [self getDataWithComponentNoLinked:component];
|
}
|
|
if (componentArray.count) {
|
if (row < componentArray.count) {
|
id titleData = componentArray[row];
|
if ([titleData isKindOfClass:[NSString class]]) {
|
return titleData;
|
} else if ([titleData isKindOfClass:[NSNumber class]]) {
|
return [NSString stringWithFormat:@"%@", titleData];
|
}
|
}
|
}
|
return @"";
|
}
|
|
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
|
{
|
if(self.isLinked){
|
if(component == 0){
|
self.selectIndex1 = row;
|
self.selectIndex2 = 0;
|
}else if(component == 1){
|
self.selectIndex2 = row;
|
self.selectIndex3 = 0;
|
}else if(component == 2){
|
self.selectIndex3 = row;
|
}
|
NSLog(@"component:%ld row: %ld",(long)component,(long)row);
|
[pickerView reloadAllComponents];
|
for (NSUInteger i = 0; i < self.component; i++) {
|
if (i > component) {
|
[pickerView selectRow:0 inComponent:i animated:YES];
|
}
|
}
|
}else{
|
// [pickerView selectRow:row inComponent:component animated:YES];
|
if(component == 0){
|
self.selectIndex1 = row;
|
|
}else if(component == 1){
|
self.selectIndex2 = row;
|
}else if(component == 2){
|
self.selectIndex3 = row;
|
}
|
}
|
|
|
// // show select content
|
// NSMutableString *selectString = [[NSMutableString alloc] init];
|
|
// reload all component and scroll to top for sub component
|
// [pickerView reloadAllComponents];
|
// for (NSUInteger i = 0; i < self.component; i++) {
|
// if (i > component) {
|
// [pickerView selectRow:0 inComponent:i animated:YES];
|
// }
|
//
|
// if (self.isShowSelectContent) {
|
// [selectString appendString:[self pickerView:pickerView titleForRow:[pickerView selectedRowInComponent:i] forComponent:i]];
|
// if (i == self.component - 1) {
|
// self.tipLabel.text = selectString;
|
// }
|
// }
|
// }
|
}
|
|
- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component
|
{
|
return self.oneComponentRowHeight;
|
}
|
|
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
|
{
|
// set separateline color
|
if (NO == self.isSettedSelectRowLineBackgroundColor) {
|
UIView *topSeparateLine = [pickerView.subviews objectAtIndex:1];
|
UIView *bottomSeparateLine = [pickerView.subviews objectAtIndex:2];
|
if (topSeparateLine.frame.size.height < 1.0f &&
|
bottomSeparateLine.frame.size.height < 1.0f) {
|
topSeparateLine.backgroundColor = self.selectRowLineBackgroundColor;
|
bottomSeparateLine.backgroundColor = self.selectRowLineBackgroundColor;
|
self.isSettedSelectRowLineBackgroundColor = YES;
|
} else {
|
for (UIView *singleLine in pickerView.subviews) {
|
if (singleLine.frame.size.height < 1.0f) {
|
singleLine.backgroundColor = self.selectRowLineBackgroundColor;
|
self.isSettedSelectRowLineBackgroundColor = YES;
|
}
|
}
|
}
|
}
|
|
// custom pickerView content label
|
UILabel *pickerLabel = (UILabel *)view;
|
|
// discussion: this is always nil, not Reusing, it's an iOS System bug.
|
// reference: https://stackoverflow.com/questions/20635949/reusing-view-in-uipickerview-with-ios-7/21039321#21039321
|
if (!pickerLabel) {
|
pickerLabel = [[UILabel alloc] init];
|
pickerLabel.textAlignment = NSTextAlignmentCenter;
|
pickerLabel.adjustsFontSizeToFitWidth = YES;
|
pickerLabel.backgroundColor = [UIColor clearColor];
|
}
|
pickerLabel.attributedText = [self pickerView:pickerView attributedTitleForRow:row forComponent:component];
|
|
return pickerLabel;
|
}
|
//
|
- (nullable NSAttributedString *)pickerView:(UIPickerView *)pickerView attributedTitleForRow:(NSInteger)row forComponent:(NSInteger)component
|
{
|
// NSString *normalRowString = [self pickerView:pickerView titleForRow:row forComponent:component];
|
// NSString *selectRowString = [self pickerView:pickerView titleForRow:[pickerView selectedRowInComponent:component] forComponent:component];
|
//
|
// NSLog(@"selectedRowInComponent ROW :%ld selectRowString:%@", [pickerView selectedRowInComponent:component],selectRowString);
|
// if (row == [pickerView selectedRowInComponent:component]) {
|
// return [[NSAttributedString alloc] initWithString:selectRowString attributes:self.selectRowTitleAttribute];
|
// } else {
|
// return [[NSAttributedString alloc] initWithString:normalRowString attributes:self.unSelectRowTitleAttribute];
|
// }
|
//
|
NSString *normalRowString = [self pickerView:pickerView titleForRow:row forComponent:component];
|
return [[NSAttributedString alloc] initWithString:normalRowString attributes:self.selectRowTitleAttribute];
|
}
|
|
#pragma mark click cance/sure button
|
- (void)userAction:(UIButton *)sender
|
{
|
// hide
|
[self zj_hide];
|
// click sure
|
if (sender.tag == 1) {
|
// completion callback
|
if (self.completion) {
|
self.completion(self.selectIndex1, self.selectIndex2, self.selectIndex3);
|
}
|
}
|
}
|
|
#pragma mark touch backgroundView
|
- (void)touchBackgroundView
|
{
|
if (self.isTouchBackgroundHide) {
|
[self zj_hide];
|
}
|
}
|
|
#pragma mark - show & hide method
|
+ (void)zj_showWithDataList:(nonnull NSArray *)dataList
|
propertyDict:(nullable NSDictionary *)propertyDict
|
completion:(nullable void(^)(NSInteger, NSInteger, NSInteger))completion
|
{
|
// no data
|
if (!dataList || dataList.count == 0) {
|
return;
|
}
|
|
|
NSDictionary *propertyDict1 = @{
|
// ZJPickerViewPropertyCanceBtnTitleKey : @"",
|
ZJPickerViewPropertyIsTouchBackgroundHideKey : @YES,
|
ZJPickerViewPropertyIsShowSelectContentKey : @YES,
|
ZJPickerViewPropertyIsScrollToSelectedRowKey: @YES,
|
ZJPickerViewPropertyIsAnimationShowKey : @YES,
|
|
};
|
|
// handle data
|
[[self sharedView] initDefaultConfig];
|
[self sharedView].tipLabel.text = @"";
|
[[self sharedView].dataList removeAllObjects];
|
[[self sharedView].dataList addObjectsFromArray:dataList];
|
[[self sharedView].propertyDict removeAllObjects];
|
[[self sharedView].propertyDict addEntriesFromDictionary:propertyDict];
|
[[self sharedView].propertyDict addEntriesFromDictionary:propertyDict1];
|
[[self sharedView] updateCustomProperiesSetter];
|
|
// calculate component num
|
id data = dataList.firstObject;
|
if ([data isKindOfClass:[NSString class]] ||
|
[data isKindOfClass:[NSNumber class]] ) {
|
[self sharedView].component = 1;
|
} else if ([data isKindOfClass:[NSDictionary class]]) {
|
[self sharedView].component++;
|
[self handleDictDataList:dataList];
|
} else {
|
NSLog(@"ZJPickerView error tip:\"Unsupported data type\"");
|
return;
|
}
|
|
// discussion: reload component in main queue, fix the first scroll to select line error bug.
|
// scorll all component to selectedRow/top
|
dispatch_async(dispatch_get_main_queue(), ^{
|
[[self sharedView].pickerView reloadAllComponents];
|
if ([self sharedView].isScrollToSelectedRow) {
|
[[self sharedView] scrollToSelectedRow];
|
} else {
|
for (NSUInteger i = 0; i < [self sharedView].component; i++) {
|
[[self sharedView].pickerView selectRow:0 inComponent:i animated:NO];
|
}
|
}
|
});
|
|
// complete block
|
if (completion) {
|
[self sharedView].completion = completion;
|
}
|
|
// show
|
if ([self sharedView].isAnimationShow) {
|
[[[[UIApplication sharedApplication] delegate] window] addSubview:[self sharedView]];
|
|
[self sharedView].backgroundView.alpha = 0.0f;
|
|
CGRect frame = [self sharedView].contentView.frame;
|
frame.origin.y = [self sharedView].frame.size.height;
|
[self sharedView].contentView.frame = frame;
|
|
__weak typeof(self) weakself = self;
|
[UIView animateWithDuration:0.3f animations:^{
|
CGRect frame = [weakself sharedView].contentView.frame;
|
frame.origin.y = [weakself sharedView].frame.size.height - [weakself sharedView].contentView.frame.size.height;
|
[weakself sharedView].contentView.frame = frame;
|
[weakself sharedView].backgroundView.alpha = [weakself sharedView].backgroundAlpha;
|
}];
|
} else {
|
[UIView animateWithDuration:0.3f animations:^{
|
[[[[UIApplication sharedApplication] delegate] window] addSubview:[self sharedView]];
|
}];
|
}
|
}
|
|
- (void)zj_hide
|
{
|
__weak typeof(self) weakself = self;
|
if (self.isAnimationShow) {
|
CGRect frame = self.contentView.frame;
|
frame.origin.y = self.frame.size.height;
|
[UIView animateWithDuration:0.3f animations:^{
|
weakself.contentView.frame = frame;
|
weakself.backgroundView.alpha = 0.0f;
|
} completion:^(BOOL finished) {
|
[weakself removeFromSuperview];
|
}];
|
} else {
|
[UIView animateWithDuration:0.3f animations:^{
|
[weakself removeFromSuperview];
|
}];
|
}
|
}
|
|
#pragma mark - private method
|
+ (void)handleDictDataList:(NSArray *)list
|
{
|
id data = list.firstObject;
|
if ([data isKindOfClass:[NSDictionary class]]) {
|
NSDictionary *dict = data;
|
id value = dict.allValues.firstObject;
|
if ([value isKindOfClass:[NSArray class]]) {
|
[self sharedView].component++;
|
[self handleDictDataList:value];
|
}
|
}
|
}
|
|
- (NSArray *)getDataWithComponent:(NSInteger)component
|
{
|
NSMutableArray *tempArray = [NSMutableArray arrayWithArray:self.dataList];
|
NSMutableArray *arrayM = [NSMutableArray array];
|
for (NSInteger i = 0; i <= component; i++) {
|
if (i == component) {
|
id data = tempArray.firstObject;
|
if ([data isKindOfClass:[NSDictionary class]]) {
|
NSMutableArray *tempTitleArray = [NSMutableArray arrayWithArray:tempArray];
|
[tempArray removeAllObjects];
|
[tempTitleArray enumerateObjectsUsingBlock:^(NSDictionary *dict, NSUInteger idx, BOOL * _Nonnull stop) {
|
[tempArray addObjectsFromArray:dict.allKeys];
|
}];
|
[arrayM addObjectsFromArray:tempArray];
|
} else if ([data isKindOfClass:[NSString class]] ||
|
[data isKindOfClass:[NSNumber class]]){
|
[arrayM addObjectsFromArray:tempArray];
|
}
|
} else {
|
NSInteger selectRow = [self.pickerView selectedRowInComponent:i];
|
if (selectRow < tempArray.count) {
|
id data = tempArray[selectRow];
|
if ([data isKindOfClass:[NSDictionary class]]) {
|
[tempArray removeAllObjects];
|
NSDictionary *dict = data;
|
[dict.allValues enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
|
if ([obj isKindOfClass:[NSArray class]]) {
|
[tempArray addObjectsFromArray:obj];
|
} else {
|
[tempArray addObject:obj];
|
}
|
}];
|
}
|
}
|
}
|
}
|
return arrayM;
|
}
|
|
- (NSArray *)getDataWithComponentNoLinked:(NSInteger)component
|
{
|
// NSMutableArray *arrayM = [NSMutableArray array];
|
if(component == 0){
|
return self.dataList;
|
}else if(component == 1){
|
return self.mSecondDataList;
|
}else if(component == 2){
|
return self.mThirdDataList;
|
}else{
|
NSMutableArray *arrayM = [NSMutableArray array];
|
return arrayM;
|
}
|
}
|
|
|
|
#pragma mark update property
|
- (void)updateCustomProperiesSetter
|
{
|
[self.propertyDict enumerateKeysAndObjectsUsingBlock:^(id _Nonnull key, id _Nonnull obj, BOOL * _Nonnull stop) {
|
if (obj) {
|
if ([key isEqualToString:ZJPickerViewPropertyCanceBtnTitleKey]) {
|
[self.canceBtn setTitle:obj forState:UIControlStateNormal];
|
} else if ([key isEqualToString:ZJPickerViewPropertySureBtnTitleKey]) {
|
[self.sureBtn setTitle:obj forState:UIControlStateNormal];
|
} else if ([key isEqualToString:ZJPickerViewPropertyTipLabelTextKey]) {
|
self.tipLabel.text = obj;
|
} else if ([key isEqualToString:ZJPickerViewPropertyDividedSymbolKey]) {
|
self.dividedSymbol = obj;
|
} else if ([key isEqualToString:ZJPickerViewPropertyCanceBtnTitleColorKey]) {
|
[self.canceBtn setTitleColor:obj forState:UIControlStateNormal];
|
} else if ([key isEqualToString:ZJPickerViewPropertySureBtnTitleColorKey]) {
|
[self.sureBtn setTitleColor:obj forState:UIControlStateNormal];
|
} else if ([key isEqualToString:ZJPickerViewPropertyTipLabelTextColorKey]) {
|
self.tipLabel.textColor = obj;
|
} else if ([key isEqualToString:ZJPickerViewPropertyLineViewBackgroundColorKey]) {
|
self.lineView.backgroundColor = obj;
|
} else if ([key isEqualToString:ZJPickerViewPropertyCanceBtnTitleFontKey]) {
|
[self.canceBtn.titleLabel setFont:obj];
|
} else if ([key isEqualToString:ZJPickerViewPropertySureBtnTitleFontKey]) {
|
[self.sureBtn.titleLabel setFont:obj];
|
} else if ([key isEqualToString:ZJPickerViewPropertyTipLabelTextFontKey]) {
|
[self.tipLabel setFont:obj];
|
} else if ([key isEqualToString:ZJPickerViewPropertyPickerViewHeightKey]) {
|
self.pickerViewHeight = [obj floatValue];
|
CGRect frame = self.pickerView.frame;
|
frame.size.height = self.pickerViewHeight;
|
self.pickerView.frame = frame;
|
|
frame = self.contentView.frame;
|
frame.size.height = toolViewHeight + self.pickerViewHeight;
|
frame.origin.y = self.frame.size.height - frame.size.height;
|
self.contentView.frame = frame;
|
} else if ([key isEqualToString:ZJPickerViewPropertyOneComponentRowHeightKey]) {
|
self.oneComponentRowHeight = [obj floatValue];
|
} else if ([key isEqualToString:ZJPickerViewPropertySelectRowTitleAttrKey]) {
|
self.selectRowTitleAttribute = obj;
|
} else if ([key isEqualToString:ZJPickerViewPropertyUnSelectRowTitleAttrKey]) {
|
self.unSelectRowTitleAttribute = obj;
|
} else if ([key isEqualToString:ZJPickerViewPropertySelectRowLineBackgroundColorKey]) {
|
self.selectRowLineBackgroundColor = obj;
|
} else if ([key isEqualToString:ZJPickerViewPropertyIsTouchBackgroundHideKey]) {
|
self.isTouchBackgroundHide = [obj boolValue];
|
} else if ([key isEqualToString:ZJPickerViewPropertyIsShowTipLabelKey]) {
|
self.isShowTipLabel = [obj boolValue]; // NO
|
self.tipLabel.hidden = self.isShowSelectContent ? NO : !self.isShowTipLabel;
|
} else if ([key isEqualToString:ZJPickerViewPropertyIsShowSelectContentKey]) {
|
self.isShowSelectContent = [obj boolValue]; // NO
|
if (self.isShowSelectContent) {
|
self.tipLabel.hidden = NO;
|
}
|
} else if ([key isEqualToString:ZJPickerViewPropertyIsScrollToSelectedRowKey]) {
|
self.isScrollToSelectedRow = [obj boolValue];
|
} else if ([key isEqualToString:ZJPickerViewPropertyIsDividedSelectContentKey]) {
|
self.isDividedSelectContent = [obj boolValue];
|
} else if ([key isEqualToString:ZJPickerViewPropertyBackgroundAlphaKey]) {
|
self.backgroundAlpha = [obj floatValue];
|
self.backgroundView.alpha = self.backgroundAlpha;
|
} else if ([key isEqualToString:ZJPickerViewPropertyIsAnimationShowKey]) {
|
self.isAnimationShow = [obj boolValue];
|
} else if ([key isEqualToString:ZJPickerViewPropertyDefaultSelectedIndexKey]) {
|
|
NSArray *array = [obj componentsSeparatedByString:@"-"]; //分割数组
|
if(array.count == 1){
|
self.selectIndex1 = [array[0] intValue];
|
}else if(array.count == 2){
|
self.selectIndex1 = [array[0] intValue];
|
self.selectIndex2 = [array[1] intValue];
|
}else if(array.count == 3){
|
self.selectIndex1 = [array[0] intValue];
|
self.selectIndex2 = [array[1] intValue];
|
self.selectIndex3 = [array[2] intValue];
|
}
|
}
|
}
|
}];
|
}
|
|
/*
|
* 2019-09-05 根据索引选中,替换原来根据传入字符选中方案
|
*/
|
- (void)scrollToSelectedRow
|
{
|
for (NSUInteger i = 0; i < self.component; i++) {
|
if(i == 0){
|
[self.pickerView selectRow:self.selectIndex1 inComponent:i animated:NO];
|
}else if(i == 1){
|
[self.pickerView selectRow:self.selectIndex2 inComponent:i animated:NO];
|
}else if(i == 2){
|
[self.pickerView selectRow:self.selectIndex3 inComponent:i animated:NO];
|
}
|
|
}
|
}
|
|
//- (void)scrollToSelectedRow
|
//{
|
//
|
//
|
// for (NSUInteger i = 0; i < self.component; i++) {
|
// if(i == 0){
|
// [self.pickerView selectRow:self.selectIndex1 inComponent:i animated:NO];
|
// }else if(i == 1){
|
// [self.pickerView selectRow:self.selectIndex2 inComponent:i animated:NO];
|
// }else if(i == 2){
|
// [self.pickerView selectRow:self.selectIndex3 inComponent:i animated:NO];
|
// }
|
//
|
// }
|
//
|
// NSString *selectedContent = self.propertyDict[ZJPickerViewPropertyTipLabelTextKey];
|
// NSMutableArray *selectContentList = [NSMutableArray arrayWithCapacity:self.component];
|
// if (self.isDividedSelectContent) {
|
// // reference: https://github.com/Abnerzj/ZJPickerView/issues/8
|
// NSArray *tempSelectContentList = [selectedContent componentsSeparatedByString:self.dividedSymbol];
|
// if (tempSelectContentList && tempSelectContentList.count == self.component) {
|
// [selectContentList addObjectsFromArray:tempSelectContentList];
|
// }
|
// }
|
// if (selectedContent.length && ![selectedContent isEqualToString:@""]) {
|
// __weak typeof(self) weakself = self;
|
// NSMutableArray *tempSelectedRowArray = [NSMutableArray arrayWithCapacity:self.component];
|
// for (NSUInteger i = 0; i < self.component; i++) {
|
// NSArray *componentArray = [self getDataWithComponent:i];
|
// if (componentArray.count) {
|
// [componentArray enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL * _Nonnull stop) {
|
// NSString *title = @"";
|
// if ([obj isKindOfClass:[NSString class]]) {
|
// title = obj;
|
// } else if ([obj isKindOfClass:[NSNumber class]]) {
|
// title = [obj stringValue];
|
// }
|
// if (![title isEqualToString:@""]) {
|
// BOOL isCanScrollToSelectePosition = NO;
|
// if (selectContentList.count > 0) {
|
// isCanScrollToSelectePosition = [selectContentList[i] isEqualToString:title];
|
// } else {
|
// // discussion: scroll to select row error when solving content similar, only appear when there is only one component.
|
// // reference: https://github.com/Abnerzj/ZJPickerView/issues/4
|
// // https://github.com/Abnerzj/ZJPickerView/issues/5
|
// NSRange range = [selectedContent rangeOfString:title];
|
// isCanScrollToSelectePosition = (self.component == 1) ? ([selectedContent isEqualToString:title]) : (range.location != NSNotFound);
|
// }
|
//
|
// if (isCanScrollToSelectePosition) {
|
// [tempSelectedRowArray addObject:@(idx)];
|
// [weakself.pickerView reloadComponent:i];
|
// [weakself.pickerView selectRow:idx inComponent:i animated:NO];
|
// [weakself.pickerView reloadComponent:i];
|
// *stop = YES;
|
// }
|
// }
|
// }];
|
// }
|
// }
|
//
|
// if (tempSelectedRowArray.count != self.component) {
|
// for (NSUInteger i = 0; i < self.component; i++) {
|
// [self.pickerView selectRow:0 inComponent:i animated:NO];
|
// }
|
// }
|
// }
|
//}
|
|
- (NSArray *)getDiffLanguageCanceAndSureBtnTitles
|
{
|
NSString *languageName = [[[NSUserDefaults standardUserDefaults] objectForKey:@"AppleLanguages"] objectAtIndex:0];
|
|
// 简体中文
|
if ([languageName rangeOfString:@"zh-Hans"].location != NSNotFound) {
|
return @[@"取消", @"确定"];
|
}
|
|
// 繁体中文
|
if ([languageName rangeOfString:@"zh-Hant"].location != NSNotFound) {
|
return @[@"取消", @"確定"];
|
}
|
|
// Other language
|
return @[@"Cance", @"Sure"];
|
}
|
|
|
/*
|
*2019-09-12 新增支持不联动效果
|
*/
|
+ (void)zj_showWithDataListWithNotLinked:(nonnull NSArray *)dataList
|
mSecondList:(nonnull NSArray *)mSecondList
|
mThirdList:(nullable NSArray *)mThirdList
|
propertyDict:(nullable NSDictionary *)propertyDict
|
completion:(nullable void(^)(NSInteger, NSInteger, NSInteger))completion
|
{
|
// no data
|
if (!dataList || dataList.count == 0) {
|
return;
|
}
|
|
// no data
|
if (!mSecondList || mSecondList.count == 0) {
|
return;
|
}
|
|
|
|
NSDictionary *propertyDict1 = @{
|
// ZJPickerViewPropertyCanceBtnTitleKey : @"",
|
ZJPickerViewPropertyIsTouchBackgroundHideKey : @YES,
|
ZJPickerViewPropertyIsShowSelectContentKey : @YES,
|
ZJPickerViewPropertyIsScrollToSelectedRowKey: @YES,
|
ZJPickerViewPropertyIsAnimationShowKey : @YES,
|
|
};
|
|
// handle data
|
[[self sharedView] initDefaultConfig];
|
[self sharedView].tipLabel.text = @"";
|
[[self sharedView].dataList removeAllObjects];
|
[[self sharedView].mSecondDataList removeAllObjects];
|
[[self sharedView].mThirdDataList removeAllObjects];
|
[[self sharedView].dataList addObjectsFromArray:dataList];
|
[[self sharedView].mSecondDataList addObjectsFromArray:mSecondList];
|
[[self sharedView].mThirdDataList addObjectsFromArray:mThirdList];
|
[[self sharedView].propertyDict removeAllObjects];
|
[[self sharedView].propertyDict addEntriesFromDictionary:propertyDict];
|
[[self sharedView].propertyDict addEntriesFromDictionary:propertyDict1];
|
[[self sharedView] updateCustomProperiesSetter];
|
|
|
//配置关键数据
|
if (!mThirdList || mThirdList.count == 0) {
|
[self sharedView].component = 2;
|
}else{
|
[self sharedView].component = 3;
|
}
|
|
[self sharedView].isLinked = NO;
|
|
|
// discussion: reload component in main queue, fix the first scroll to select line error bug.
|
// scorll all component to selectedRow/top
|
dispatch_async(dispatch_get_main_queue(), ^{
|
[[self sharedView].pickerView reloadAllComponents];
|
if ([self sharedView].isScrollToSelectedRow) {
|
[[self sharedView] scrollToSelectedRow];
|
} else {
|
for (NSUInteger i = 0; i < [self sharedView].component; i++) {
|
[[self sharedView].pickerView selectRow:0 inComponent:i animated:NO];
|
}
|
}
|
});
|
|
// complete block
|
if (completion) {
|
[self sharedView].completion = completion;
|
}
|
|
// show
|
if ([self sharedView].isAnimationShow) {
|
[[[[UIApplication sharedApplication] delegate] window] addSubview:[self sharedView]];
|
|
[self sharedView].backgroundView.alpha = 0.0f;
|
|
CGRect frame = [self sharedView].contentView.frame;
|
frame.origin.y = [self sharedView].frame.size.height;
|
[self sharedView].contentView.frame = frame;
|
|
__weak typeof(self) weakself = self;
|
[UIView animateWithDuration:0.3f animations:^{
|
CGRect frame = [weakself sharedView].contentView.frame;
|
frame.origin.y = [weakself sharedView].frame.size.height - [weakself sharedView].contentView.frame.size.height;
|
[weakself sharedView].contentView.frame = frame;
|
[weakself sharedView].backgroundView.alpha = [weakself sharedView].backgroundAlpha;
|
}];
|
} else {
|
[UIView animateWithDuration:0.3f animations:^{
|
[[[[UIApplication sharedApplication] delegate] window] addSubview:[self sharedView]];
|
}];
|
}
|
}
|
|
|
|
@end
|