CustomPickerView.h
定義pickerView選中item的代理
@protocol MyPickerViewDelegate <NSObject>
@optional
- (void)pickerView:(UIPickerView *)pickerView didSelectRow: (NSInteger)row;
@end
CustomPickerView.m
定義item高度及遵守pickerview代理
#define itemHeight 50
@interface CustomPickerView ()<UIPickerViewDelegate,UIPickerViewDataSource>
@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);
//旋轉(zhuǎn) -π/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;//隱藏分割線
}
}
}
}
}
}
}
}