我只在极少数 iPhone 应用程序中看到过它……但它看起来像一个向左/向右旋转(而不是顶部/底部)的选择器。
他们通常将其放在 tableView 的 1 行...以允许用户在少量选择(如 3-10)之间快速选择。
那是怎么编码的?
我只在极少数 iPhone 应用程序中看到过它……但它看起来像一个向左/向右旋转(而不是顶部/底部)的选择器。
他们通常将其放在 tableView 的 1 行...以允许用户在少量选择(如 3-10)之间快速选择。
那是怎么编码的?
在这里,您将找到水平对齐的 Picker 源代码。
继续 Dave DeLong 的回答,我让它像这样工作......
在viewDidLoad
我这样做...
CGRect frame = horizontalPickerView.frame;
frame.size.width = 50;
frame.size.height = 216;
frame.origin.x=90;
frame.origin.y = 200;
horizontalPickerView.frame = frame;
horizontalPickerView.transform = CGAffineTransformMakeRotation(3.14159/2);
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
UILabel *lbl = [[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 30, 20)] autorelease];
lbl.transform = CGAffineTransformMakeRotation(-3.14159/2);
lbl.text = @"hi";
return lbl;
}
希望这可以帮助
您可以通过使用常规 UIPickerView,调整其宽度(通过setFrame:
),然后应用 NSAffineTransform 将其旋转 90º 来做到这一点。然后,您需要将选择器中的每个项目以另一种方式旋转 90º。
正确地做到这一点有点乏味,但它是可以做到的。
@Madhup 的代码在我搜索水平方向时将我引向了我想要的大致方向,UIPickerView
但后来我意识到所提出的问题并没有真正得到解决,因此对于那些正在寻找更合适的从左到右旋转答案的人来说。我读过的答案中的代码都是为了启用从左到右的滑动,导致选择器将具有更高值的标签/行推到视图的左侧。任何方式都是我的贡献:
在viewDidLoad
方法中:
yourPickerView.frame = frame;
yourPickerView.transform = CGAffineTransformMakeRotation(4.71238898); //Instead of rotating clockwise 90° we're rotating 90° counterclockwise. 4.71238898 being ≈270° in radians.
[self.view addSubview:self.picker];
self.yourPickerView.delegate = self;
self.yourPickerView.dataSource = self;
self.yourPickerView.showsSelectionIndicator = YES;
self.yourPickerView.userInteractionEnabled = YES;
的pickerView
方法:
-(UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
UILabel *label = [[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 20, 20)] autorelease];
yourLabel.transform = CGAffineTransformMakeRotation(1.57079633); //Instead of rotating counterclockwise 90° we're rotating 90° clockwise 1.57079633 being ≈90° in radians.
yourLabel.font = [UIFont fontWithName:@"System-Bold" size:18]; //Your font here.
yourLabel.text = @"yourLabel's text"; //or like me [NSString stringWithFormat:@"%@, [yourArray objectAtIndex:row]]
yourLabel.backgroundColor = [UIColor clearColor];
return label;
}
尝试一个分页滚动视图,它在每页上包含您想要的项目,如果您想要更好的控件图形,并且只允许水平滚动(不要使滚动视图的 contentSize 高于实际视图的大小,并禁用控件上的垂直滚动弹跳)。
您必须以编程方式创建选择器,以便您可以创建自己大小的选择器,CGRectMake(x, y, width, height)
然后您必须旋转它,但旋转它也会在 Picker 的 dataSources 方法中旋转,您必须旋转与选择器旋转相反的视图,我希望包含代码它会有所帮助
.....
...
...
NSArray *arr = [NSArray arrayWithObjects:@"1 mi", @"2 mi", @"5 mi", @"10 mi", @"15 mi", @"20 mi", @"25 mi",
@"30 mi", @"35 mi", @"40 mi", @"45 mi", @"50 mi", @"75 mi", @"99 mi", nil];
radiusDefaults = [[NSMutableArray alloc] initWithArray:arr] ;
radiusPicker = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 0, 100, 150)];
radiusPicker.delegate = self;
radiusPicker.dataSource = self;
radiusPicker.showsSelectionIndicator = NO;
//Resize the picker, rotate it so that it is horizontal and set its position
CGAffineTransform rotate = CGAffineTransformMakeRotation(-1.57);
rotate = CGAffineTransformScale(rotate, .1, .5);
CGAffineTransform t0 = CGAffineTransformMakeTranslation(-61, 0);
radiusPicker.transform = CGAffineTransformConcat(rotate,t0);
// [theNavigationBar.topItem setTitleView:radiusPicker] ;
UIView *pickerWrapper = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 215)];
[self.view addSubview:radiusPicker];
[radiusPicker selectRow:6 inComponent:0 animated:NO];
[radiusPicker release];
.....
.......
....
#pragma mark -
#pragma mark UIPickerView
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row
forComponent:(NSInteger)component reusingView:(UIView *)view{
UIView *viewForRow = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 70, 400)] autorelease];
UILabel *label;
UIFont *font = [ UIFont fontWithName:@"ArialRoundedMTBold" size:22];
label = [[[UILabel alloc] initWithFrame:CGRectMake(0, 20, 70, 350)] autorelease];
[label setText:[NSString stringWithFormat:@"%@", [radiusDefaults objectAtIndex:row]]];
label.textAlignment = UITextAlignmentCenter;
label.textColor = [UIColor blueColor];
label.font = font;
label.backgroundColor = [UIColor clearColor];
// label.opaque = NO;
[viewForRow addSubview:label];
CGAffineTransform rotate = CGAffineTransformMakeRotation(1.57);
rotate = CGAffineTransformScale(rotate, 1, 6.5);
[viewForRow setTransform:rotate];
return viewForRow;
}
试试这个-> 创建 UIPickerview 大小的普通 UIVew,在上面添加选择器。设置 numberOfComponentsInPickerView = 1。设置组件宽度。然后在其上添加小的子视图以隐藏选择器的其余部分。只有组件的旋转轮应可见。变换普通视图以将其旋转 90 度。
确保在以下位置应用转换:
-(UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
UILabel *lbl = nil;
if(view)
{
lbl = (UILabel *) [view viewWithTag:11];
}
else
{
view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 32, 32)];
lbl = [[UILabel alloc] initWithFrame:CGRectMake(1, 0, 30, 30)];
lbl.tag = 11;
lbl.backgroundColor = [UIColor clearColor];
lbl.textAlignment = UITextAlignmentCenter;
lbl.font = [UIFont fontWithName:@"Helvetica-Bold" size:18];
lbl.transform = CGAffineTransformRotate(lbl.transform, M_PI +M_PI/2);
[view addSubview:lbl];
[lbl release];
}
lbl.text = [dataSourceArray objectAtIndex:row];
return view;
}
现在,您可以将 palin 视图添加为任何视图上水平选择器的子视图。
您是否考虑过获取表格视图并旋转它?
没想到。去试试吧。:)