{/**值是字典 或是字符串的标志*/
int flag ;
}
/***获取单列中的标题数组*/
@property(strong,nonatomic)NSMutableArray *titlesArr;
/**传递的是重组数组*/
/**顶部背景视图*/
@property(strong,nonatomic)UIView *backTv;
/**底部背景视图*/
@property(strong,nonatomic)UIView *backBv;
/**选中的字符串*/
@property(strong,nonatomic)NSString *selecStr;
/**选中的ID*/
@property(strong,nonatomic)NSString *selecId;
@end
@interface CustomPickV ()<UIPickerViewDataSource,UIPickerViewDelegate>
{
CGFloat view_w;
CGFloat view_h;
CGFloat row_h;
}
@end
@implementation CustomPickV
-(instancetype)initWithFrame:(CGRect)frame andSetValueWithArr:(NSArray *)Arr
{
if (self=[super initWithFrame:frame]) {
self.titlesArr=[[NSMutableArray alloc]init];
view_w=frame.size.width;
view_h=frame.size.height;
if (Arr.count!=0&&Arr!=nil) {
/**根据数组的大小 给pickv赋高度*/
row_h=Arr.count*65;
for (id obj in Arr) {/**判断数组中的值类型*/
if ([obj isKindOfClass:[NSDictionary class]]) {
flag=33;
NSDictionary *dic=(NSDictionary *)obj;
/**将字典加入数组中*/
[self.titlesArr addObject:dic];
}
/**如果是字符串类型 直接赋值 就好*/
if ([obj isKindOfClass:[NSString class]]) {
flag=34;
self.titlesArr=Arr.mutableCopy;
}
}
}
else{
/**如果数组为空 则不出现选择框*/
return nil;
}
/**表示是字符串*/
if (flag==34) {
self.selecStr=self.titlesArr.firstObject;
}
if (flag==33) {/**字典中的值*/
self.selecStr=self.titlesArr.firstObject[@"name"];
self.selecId=self.titlesArr.firstObject[@"id"];
}
self.backgroundColor = [[UIColor colorWithRed:0 green:0 blue:0 alpha:0.45]colorWithAlphaComponent:0.35];/**设置父视图灰显子视图不变色*/
[self addSubview:self.custompV];
[self addSubview:self.backBv];
[self addSubview:self.backTv];
}
return self;
}
/**标题*/
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
if (flag==33) {
// return self.titlesArr.firstObject[@"name"];
return self.titlesArr[row][@"name"];
}
else
return self.titlesArr[row];
}
/**
*
*重新设置pickview的lab;
* @param pickerView
* @param row
* @param component
* @param view
*
* @return pickv的显示文本
*/
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
UILabel* pickerLabel = (UILabel*)view;//强制转换成lab
if (!pickerLabel){
pickerLabel = [[UILabel alloc] init];
pickerLabel.font = [UIFont systemFontOfSize:12.0];
pickerLabel.adjustsFontSizeToFitWidth = YES;//让他适应文本的宽度
[pickerLabel setTextAlignment:NSTextAlignmentCenter];//居中
[pickerLabel setBackgroundColor:[UIColor clearColor]];
[pickerLabel setFont:[UIFont boldSystemFontOfSize:15]];
/**
1 + systemFontOfSize
2 + boldSystemFontOfSize
3 + italicSystemFontOfSize
1 中是系统的默认方法
2 是字体 bold :醒目的;雄浑的;突出的,使用后会加粗字体
3 字体为斜体 italic [i'tælik] n. 斜体,斜体字
*/
}
pickerLabel.text=[self pickerView:pickerView titleForRow:row forComponent:component];
return pickerLabel;
}
/***多少列*/
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 1;
}
/**
*单元格点击事件
*
* @param pickerView
* @param row
* @param component
*/
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
// self.selecStr=self.titlesArr[row];
//
//
//
// [self.delegate customPickvSelect:self.selecStr];
if (flag==33) {
self.selecStr=self.titlesArr[row][@"name"];
self.selecId=self.titlesArr[row][@"id"];
// [self.delegate customPickvSelect:self.selecStr andselID:_selecId];
}
else
CLog(@"wawawa%@",self.titlesArr[row]);
self.selecStr=self.titlesArr[row];
}
/***单列里面有多少行*/
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
return self.titlesArr.count;
}
/**点击时 此视图消失*/
-(void)backvTapAction
{
[self.delegate customPickvSelect:self.selecStr andselID:self.selecId];
/**点击完成之后出现动画 */
[UIView animateWithDuration:0.8f delay:0 usingSpringWithDamping:0.9f initialSpringVelocity:0.7f options:UIViewAnimationOptionCurveEaseInOut | UIViewAnimationOptionBeginFromCurrentState | UIViewAnimationOptionLayoutSubviews animations:^{
self.alpha = 0.0;
self.frame = CGRectMake(0, view_h/3.0,iphone_w , 250);
} completion:^(BOOL finished) {
/**移除*/
[self removeFromSuperview];
}];
}
/**
*
*
* @param pickerView
* @param component 每列
*
* @return 宽度
*/
-(CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component
{
return 100;
}
/**格子的高度*/
- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component
{
return 65;
}
-(UIPickerView *)custompV
{
if (!_custompV) {
_custompV=[[UIPickerView alloc]initWithFrame:CGRectMake(0, view_h/3, view_w, row_h)];
_custompV.dataSource=self;
_custompV.delegate=self;
_custompV.backgroundColor=[UIColor whiteColor];
_custompV.showsSelectionIndicator=YES;
_custompV.autoresizingMask = UIViewAutoresizingFlexibleWidth;
}
return _custompV;
}
-(UIView *)backTv
{
if (!_backTv) {
_backTv=[[UIView alloc]initWithFrame:Frame(0,0, view_w,view_h/3.0)];
_backBv.backgroundColor=[UIColor whiteColor];
_backBv.alpha=0.25;
UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(backvTapAction)];
[_backTv addGestureRecognizer:tap];
}
return _backTv;
}
-(UIView *)backBv
{
if (!_backBv) {
_backBv=[[UIView alloc]initWithFrame:Frame(0, view_h/3.0+row_h, view_w, view_h/2.0-row_h)];
_backBv.backgroundColor=[UIColor whiteColor];
_backBv.alpha=0.25;
UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(backvTapAction)];
[_backBv addGestureRecognizer:tap];
}
return _backBv;
}