自定义横向滚动pickerview

CustomPickerView.h

定义pickerView选中item的代理

@protocol MyPickerViewDelegate 
@optional
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:  (NSInteger)row;
@end

CustomPickerView.m

定义item高度及遵守pickerview代理
#define itemHeight 50
@interface CustomPickerView ()
@end

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self performSelector:@selector(initPickerView)];
    }
    return self;
}

初始化 选择器

-(void)initPickerView{

CGAffineTransform rotate = CGAffineTransformMakeRotation(-M_PI/2);
rotate = CGAffineTransformScale(rotate, 0.1, 1);
//旋转 -π/2角度
picker = [[UIPickerView alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.height*10, self.frame.size.width)];

[picker setTag: 10086];
picker.delegate = self;
picker.dataSource = self;
picker.showsSelectionIndicator = false;
[picker setBackgroundColor:[UIColor clearColor]];

UIImageView *imageV = [[UIImageView alloc]initWithFrame:CGRectMake(self.frame.size.width/2-1.5, (self.frame.size.height- 60)/2, 3, 60)];
imageV.image = [UIImage imageNamed:@"PTZMiddleLine"];
UIView *bgV = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width,self.frame.size.height)];
[bgV addSubview:picker];
[bgV addSubview:imageV];
[self addSubview:bgV];
[picker setTransform:rotate];
picker.center = CGPointMake(self.frame.size.width / 2, self.frame.size.height / 2);

}

pickerView有多少个元素

-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
return [dataArray count];
}

pickerView 有多少列

-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
return 1;
}

每个 item 显示的 视图

-(UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{

CGAffineTransform rotateItem = CGAffineTransformMakeRotation(M_PI/2);
rotateItem = CGAffineTransformScale(rotateItem, 1, 10);

CGFloat width = self.frame.size.height;

UIView *itemView = [[UIView alloc]initWithFrame:CGRectMake(20, 0, width-40, 50)];
UIImageView *itemImgLeft;
UIImageView *itemImgRight;
if (row == 0) {
    itemImgLeft = [[UIImageView alloc]initWithFrame:CGRectMake(-0.5, (itemHeight -20)/2, 1, 20)];
    itemImgRight = [[UIImageView alloc]initWithFrame:CGRectMake(width-40-0., (itemHeight -20)/2, 1, 20)];
}else if(row == self.dataModel.count-1){
    itemImgLeft = [[UIImageView alloc]initWithFrame:CGRectMake(-1, (itemHeight -20)/2, 1, 20)];
    itemImgRight = [[UIImageView alloc]initWithFrame:CGRectMake(width-40-0.5, (itemHeight -21)/2, 1, 20)];
}else{
    itemImgLeft = [[UIImageView alloc]initWithFrame:CGRectMake(-1.1, (itemHeight -20)/2, 1, 20)];
    itemImgRight = [[UIImageView alloc]initWithFrame:CGRectMake(width-40+0.25, (itemHeight -20)/2, 1, 20)];
}
itemImgLeft.image = [UIImage imageNamed:@"PTZThinLine"];
itemImgRight.image = [UIImage imageNamed:@"PTZThinLine"];
[itemView addSubview:itemImgLeft];
[itemView addSubview:itemImgRight];
UIImageView *itemImgMiddle = [[UIImageView alloc]initWithFrame:CGRectMake((width-40)/2-1.5, (itemHeight -30)/2, 3, 30)];
itemImgMiddle.image = [UIImage imageNamed:@"PTZThickLine"];
[itemView addSubview:itemImgMiddle];

itemView.transform = rotateItem;
return itemView;
}

每个item的宽度

- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component __TVOS_PROHIBITED{
return self.frame.size.height;
}

每个item的高度

- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component{
return itemHeight;
}

pickerView滑动到指定位置

-(void)scrollToIndex:(NSInteger)scrollToIndex{
[picker selectRow:scrollToIndex inComponent:0 animated:true];
}

查询当前选择元素Getter方法(index:选择位置 name:元素名称)

-(NSDictionary *)selectedItem{
NSInteger index = [picker selectedRowInComponent:0];
NSString *contaxt = dataArray[index];
return @{@"name":contaxt,@"index":[NSString stringWithFormat:@"%ld",index]};
}

pickerview选中的代理
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
[self.delegate pickerView:pickerView didSelectRow:row];
}

分割线的处理,去掉分割线

for (UIView *subv1 in self.pickerBgView.subviews) {
    if ([subv1 isKindOfClass:[CustomPickerView class]]) {
        for (UIView *subv2 in subv1.subviews) {
            if ([subv2 isKindOfClass:[UIView class]]) {
                for (UIView *subv3 in subv2.subviews) {
                    if ([subv3 isKindOfClass:[UIPickerView class]]) {
                        for (UIView *subv4 in subv3.subviews) {
                            if (subv4.frame.size.height < 1)//取出分割线view
                            {
                                //NSLog(@"subv4:%@",subv4);
                                subv4.hidden = YES;//隐藏分割线
                            }
                        }
                    }
                }
            }
        }
    }
}

完整Demo地址:https://github.com/WSGNSLog/CustomPickerView

你可能感兴趣的:(自定义横向滚动pickerview)