An iphone uipickerview that rotates horizontally?

2020-05-21 04:55发布

I've only seen it in a VERY few iPhone apps... but it looks like a picker that rotates left/right (instead of top/bottom).

They usually put it on 1 line of a tableView... to allow the user to quickly pick between a small number of choices (like 3-10).

How is that coded?

8条回答
beautiful°
2楼-- · 2020-05-21 05:19

Try this-> Create Plain UIVew of size of UIPickerview, add picker on it. set numberOfComponentsInPickerView = 1. set componant width. Then add small sub views on it to Hide rest of picker. Only rotating wheel of componant should visible. Transform plain view to rotate it through 90 degree.

Make sure to apply tranform in:

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

Now you can add palin view as a subview for horizontal picker on any view.

查看更多
Fickle 薄情
3楼-- · 2020-05-21 05:21

Continuing the answer by Dave DeLong I got it working like this......

In viewDidLoad i did this...

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

Hope this helps

查看更多
登录 后发表回答