2009-12-12 92 views
8

我只看到它在极少数iPhone应用程序中...但它看起来像一个选择器左右旋转(而不是顶部/底部)。水平旋转的iphone uipickerview?

他们通常把它放在tableView的1行上,以允许用户在少数选择(如3-10)之间快速选择。

这是怎么编码的?

+0

下面是选取器的外观: http://images.macworld.com/images/reviews/graphics/143531-dr_dof_original.jpg – Donna 2009-12-12 04:23:53

回答

-12

你有没有考虑采取表视图和旋转它?

不这么认为。去试试看。 :)

+0

我可能不想旋转整个表格视图。 (并让屏幕上的“行”从上到下) 但是......也许开发人员正在旋转选取器......并且有些如何调整它以适应表格的一行 - 视图。 但代码的样子是什么? – Donna 2009-12-12 04:14:39

+0

你为什么不尝试写下它并自己找出来?这是你学习任何东西的唯一方法。 – Sneakyness 2009-12-12 04:26:15

+0

我强烈建议您阅读苹果的iPhone手机版http://developer.apple.com上的所有文档,了解如何使用iPhone编程和Objective-C进行入门。 – Sneakyness 2009-12-12 04:27:05

0

尝试一个页面滚动视图,每个页面上都有你想要的项目,如果你想为你的控件使用更好的图形,并且只允许水平滚动(不要让contentSize滚动视图的高度大于实际视图的大小,并禁用控件上的垂直滚动反弹)。

3

你可以通过一个普通的UIPickerView,调整它的宽度(通过setFrame:),然后应用一个NSAffineTransform将它旋转90º来达到这个目的。然后您需要以另一种方式旋转拾取器90º中的每个项目。

这样做有点繁琐,但可以做到。

+0

字母也不会转90度吗?我认为这是截图上的自定义视图。 – 2009-12-12 17:26:09

+0

@Alex是的,这就是为什么你需要旋转每个'viewForRow:forComponent:reusingView:'90º的另一种方式。 – 2009-12-12 18:53:32

6

继续由Dave德隆的答案,我得到它的工作是这样......

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; 
    } 

希望这有助于

+1

对于CGAffineTransform不要忘记添加QuartzCore框架到你的项目 – 2009-12-12 13:08:46

+0

你能解释一下3.14159/2吗? (我知道它的PI,但它为什么在这里,为什么/ 2)? – 2009-12-13 19:28:34

+1

我这样做,使捡取器旋转90度,因此它将水平滚动和3.14159/2是90度的辐射值 – 2009-12-14 04:09:34

0

试试这个 - > 创建UIPickerview大小的Plain UIVew,在其上添加选择器。 set 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; 
} 

现在你可以添加佩林视图作为任何水平视器中选择一种子视图。

0

您将不得不创建选择器,以便您可以创建自己的大小选择器,然后,您将不得不旋转它,但旋转它也会在选取器的dataSources方法中旋转,您将不得不旋转视图选择器的旋转,我包括代码的逆hopfully这将有助于

..... 
... 
... 
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; 
    } 
1

@ 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; 
    } 
6

Here,你会发现这是水平排列的选择器的源代码。

+0

不能水平滚动。您必须点击每个单元格才能滚动浏览。 – chicobermuda 2016-03-08 20:48:04