【问题标题】:An iphone uipickerview that rotates horizontally?水平旋转的iphone uipickerview?
【发布时间】:2009-12-12 03:40:50
【问题描述】:

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

他们通常将它放在 tableView 的 1 行...以允许用户在少量选项(如 3-10)之间快速选择。

这是怎么编码的?

【问题讨论】:

标签: iphone objective-c user-interface uipickerview


【解决方案1】:

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

希望对你有帮助

【讨论】:

  • 对于 CGAffineTransform 不要忘记将 QuartzCore 框架添加到您的项目中
  • 你能解释一下 3.14159/2 吗? (我知道它的 PI,但为什么它在这里以及为什么 /2)?
  • 我这样做是为了让选取器旋转 90 度,因此它将水平滚动,而 3.14159/2 是 90 度的辐射值
【解决方案2】:

Here 你会找到水平对齐的 Picker 源代码。

【讨论】:

  • 无法水平滚动。您必须点击每个单元格才能滚动浏览。
【解决方案3】:

您可以通过使用常规 UIPickerView,调整其宽度(通过setFrame:),然后应用 NSAffineTransform 将其旋转 90º 来做到这一点。然后,您需要将选取器中的每个项目以另一种方式旋转 90º。

正确地做有点乏味,但可以做到。

【讨论】:

  • 字母不也会转90度吗?我认为这是屏幕截图上的自定义视图。
  • @Alex 是的,这就是为什么您需要将每个 viewForRow:forComponent:reusingView: 旋转 90º。
【解决方案4】:

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

【讨论】:

    【解决方案5】:

    尝试一个分页滚动视图,它在每页上包含您想要的项目,如果您想要更好的控件图形,可能会在其上方覆盖图像,并且只允许水平滚动(不要设置滚动视图的 contentSize高于实际视图的大小,并禁用控件上的垂直滚动弹跳)。

    【讨论】:

      【解决方案6】:

      您必须以编程方式创建选择器,以便您可以使用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;
          }
      

      【讨论】:

        【解决方案7】:

        试试这个-> 创建 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 视图作为水平选择器的子视图。

        【讨论】:

          【解决方案8】:

          您是否考虑过获取表格视图并旋转它?

          没想到。去试试吧。 :)

          【讨论】:

          • 我可能不想旋转整个表格视图。 (并且让“行”在屏幕上从上到下移动。)但是......也许开发人员正在旋转选择器......以及如何调整它的大小以适应表格视图的 1 行。但是代码会是什么样子呢?
          • 您为什么不尝试编写它并自己找出答案?只有这样你才能学到任何东西。
          • 我强烈建议您在 iPhone 部分的developer.apple.com 阅读所有 Apple 文档,了解 iPhone 编程和 Objective-C。
          • 所以我的问题的答案是“去了解自己”吗?或者阅读 1000 页的文档... NONE 其中提到了“水平选择器”?哇。谢谢。我们真的根本不需要 stackoverflow.com……每个人都应该自己解决所有问题。
          • @Sneakyness 不是每个人都有时间投入到 Cocoa 中。考虑其他情况。 SO.com 的全部意义在于人们有问题,我们要么 1)知道答案,要么 2)知道如何找到它。告诉某人 RTFM 几乎从来都不是最好的答案。 ;)
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-10-08
          • 2017-12-26
          • 1970-01-01
          相关资源
          最近更新 更多