水平实现UIPickerView?

时间:2012-10-02 19:02:06

标签: iphone xcode uipickerview

UIPickerView的默认滚动设置设置为垂直。是否可以水平实施UIPickerView

如果是这样,你能不能给我一个样本或指导我在哪里提供有用的文件?

2 个答案:

答案 0 :(得分:2)

您可以使用CPPickerView .. UIPickerView的自定义,可配置,水平版本(基于旋转轮或插槽机器隐喻),带有包含的表格单元格实现。最初用于冷凝多选项设置所需的空间/行。

答案 1 :(得分:2)

IN .h文件

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController <UIPickerViewDelegate> {
    IBOutlet UIPickerView *pickerView;
    NSMutableArray *itemArray;
    IBOutlet UILabel *myLabel;
}

@property (nonatomic, retain)  UIPickerView *pickerView;
@property (nonatomic, retain)  UILabel *myLabel;

@end  

IN .XIB文件

  

拖放UIPickerView和One UILable   同时将“委托”和“引用插座”连接到FileOwner。

IN .M文件

#import "ViewController.h"

@implementation ViewController

@synthesize pickerView, myLabel;

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    self.pickerView.delegate = self;
    self.pickerView.showsSelectionIndicator =YES;
    self.pickerView.backgroundColor = [UIColor blackColor];

    CGAffineTransform rotate = CGAffineTransformMakeRotation(M_PI_2);
    rotate = CGAffineTransformScale(rotate, 0.1, 0.8);
    [self.pickerView setTransform:rotate];  

    self.pickerView.center = CGPointMake(160,75);


    UILabel *theview[20]; 
    CGAffineTransform rotateItem = CGAffineTransformMakeRotation(-M_PI_2);
    rotateItem = CGAffineTransformScale(rotateItem, 1, 10);

    for (int i=0;i<20;i++) {  
        theview[i] = [[UILabel alloc] init];
        theview[i].text = [NSString stringWithFormat:@"%d",i];
        theview[i].textColor = [UIColor blackColor];
        theview[i].frame = CGRectMake(0,0, 100, 100);
        theview[i].backgroundColor = [UIColor clearColor];
        theview[i].textAlignment = NSTextAlignmentCenter; //UITextAlignmentCenter is deprecated.
        theview[i].shadowColor = [UIColor whiteColor];
        theview[i].shadowOffset = CGSizeMake(-1,-1);
        theview[i].adjustsFontSizeToFitWidth = YES;

        UIFont *myFont = [UIFont fontWithName:@"Georgia" size:15];
        [theview[i] setFont:myFont];

        theview[i].transform = rotateItem;
    }





    itemArray = [[NSMutableArray alloc] init];

    for (int j=0;j<20;j++) { 

        [itemArray addObject:theview[j]];

    }

}


#pragma mark -
#pragma mark Picker View Methods

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)thePickerView {

    return 1;
}

- (NSInteger)pickerView:(UIPickerView *)thePickerView numberOfRowsInComponent:(NSInteger)component {

    return [itemArray count];
}



- (UIView *)pickerView:(UIPickerView *)thePickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{

    return [itemArray objectAtIndex:row];

}

- (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {

    myLabel.text = [NSString stringWithFormat:@"SELECTED: %d", row+1];
}


- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
}

- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}

@end