2011-04-20 46 views
0

我做了一个UIScrollView,它包含四个UILabel。我希望所选的UILabel不在屏幕左侧,而是在中央。我附上了两张图片来说明我的意思。在第一张图片上,绿色标签将成为选定的一张,在下一张图片上,蓝色标签将成为选定的一张。在中心选择的UIScrollView分页

有没有办法做到这一点?

这是我的代码:

[scrollView setContentSize:CGSizeMake(428, 65)]; 
[scrollView setDelegate:self]; 

UILabel *label1 = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 107, 65)]; 
[label1 setBackgroundColor:[UIColor redColor]]; 
[label1 setTextAlignment:UITextAlignmentCenter]; 
[label1 setText:@"Wednesday"]; 
[scrollView addSubview:label1]; 
[label1 release]; 

UILabel *label2 = [[UILabel alloc] initWithFrame:CGRectMake(107, 0, 107, 65)]; 
[label2 setBackgroundColor:[UIColor greenColor]]; 
[label2 setTextAlignment:UITextAlignmentCenter]; 
[label2 setText:@"Thursday"]; 
[scrollView addSubview:label2]; 
[label2 release]; 

UILabel *label3 = [[UILabel alloc] initWithFrame:CGRectMake(214, 0, 107, 65)]; 
[label3 setBackgroundColor:[UIColor blueColor]]; 
[label3 setTextAlignment:UITextAlignmentCenter]; 
[label3 setText:@"Friday"]; 
[scrollView addSubview:label3]; 
[label3 release]; 

UILabel *label4 = [[UILabel alloc] initWithFrame:CGRectMake(321, 0, 107, 65)]; 
[label4 setBackgroundColor:[UIColor orangeColor]]; 
[label4 setTextAlignment:UITextAlignmentCenter]; 
[label4 setText:@"Saturday"]; 
[scrollView addSubview:label4]; 
[label4 release]; 

Example 1 Example 2

+1

你是什么意思的“选定”?滚动查看不会做选择,它只是滚动。 – Adam 2011-04-20 10:38:51

+0

这是正确的,但我已经在scrollview中启用分页,这使得有可能给幻影选择一部分滚动视图。但是如果有其他方法可以做到这一点,我很乐意听到它。我有四个UILabels,其中三个需要一次在屏幕上可见。中间的那个是选中的那个。 – simonbs 2011-04-20 10:42:42

+0

UIScrollView的分页问题是它的contentSize定义了它的页面大小,这意味着你不能有一个完整宽度的contentSize并且以分页的方便滚动1/3的宽度... – tsakoyan 2011-04-20 11:09:54

回答

0

这似乎是最简单的旋转UIPickerView。这可以像这样完成。

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

    arrayDays = [[NSMutableArray alloc] init]; 
    [arrayDays addObject:@"ONSDAG"]; 
    [arrayDays addObject:@"TORSDAG"]; 
    [arrayDays addObject:@"FREDAG"]; 
    [arrayDays addObject:@"LØRDAG"]; 

    arrayDates = [[NSMutableArray alloc] init]; 
    [arrayDates addObject:@"29. JUNI"]; 
    [arrayDates addObject:@"30. JUNI"]; 
    [arrayDates addObject:@"1. JULI"]; 
    [arrayDates addObject:@"2. JULI"]; 

    pickerViewDay = [[UIPickerView alloc] initWithFrame:CGRectZero]; 
    [pickerViewDay setDelegate:self]; 
    [pickerViewDay setShowsSelectionIndicator:NO]; 
    CGAffineTransform rotate = CGAffineTransformMakeRotation(-M_PI/2); 
    rotate = CGAffineTransformScale(rotate, 0.25, 2.0); 
    [pickerViewDay setTransform:rotate]; 
    [pickerViewDay setCenter:CGPointMake(self.view.frame.size.width/2, (pickerViewDay.frame.size.height/2)-3)]; 
    [self.view addSubview:pickerViewDay]; 

    // Adding selection indicator to pickerview 
    UIImage *selectorImage = [UIImage imageNamed:@"DayPickerView_SelectionIndicator.png"]; 
    UIView *customSelector = [[UIImageView alloc] initWithImage:selectorImage]; 
    [customSelector setFrame:CGRectMake(0, 0, 120, 74)]; 
    [customSelector setCenter:CGPointMake(self.view.frame.size.width/2, customSelector.frame.size.height/2)]; 
    [self.view addSubview:customSelector]; 
    [customSelector release]; 

    // Adding background to pickerview 
    UIImage *backgroundImage = [UIImage imageNamed:@"DayPickerView_Background.png"]; 
    UIView *custombackground = [[UIImageView alloc] initWithImage:backgroundImage]; 
    [custombackground setFrame:CGRectMake(0, 0, 320, 74)]; 
    // [self.view addSubview:custombackground]; 
    [custombackground release]; 
} 

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view { 
    UIView *viewRow = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 150, 80)]; 

    CGAffineTransform rotate = CGAffineTransformMakeRotation(3.14/2); 
    rotate = CGAffineTransformScale(rotate, 0.25, 2.0); 

    // Date 
    CGRect rectDate = CGRectMake(30, 0, 150, 80); 
    UILabel *date = [[UILabel alloc]initWithFrame:rectDate]; 
    [date setTransform:rotate]; 
    [date setText:[arrayDates objectAtIndex:row]]; 
    [date setFont:[UIFont fontWithName:@"Arial-BoldMT" size:37.0]]; 
    [date setShadowColor:[UIColor whiteColor]]; 
    [date setShadowOffset:CGSizeMake(0, -1)]; 
    [date setTextAlignment:UITextAlignmentCenter]; 
    [date setBackgroundColor:[UIColor clearColor]]; 
    [date setClipsToBounds:YES]; 
    [viewRow addSubview:date]; 

    // Day 
    CGRect rectDay = CGRectMake(-30, 0, 150, 80); 
    UILabel *day = [[UILabel alloc]initWithFrame:rectDay]; 
    [day setTransform:rotate]; 
    [day setText:[arrayDays objectAtIndex:row]]; 
    [day setFont:[UIFont fontWithName:@"Arial-BoldMT" size:21.0]]; 
    [day setTextColor:[UIColor colorWithRed:0.35 green:0.35 blue:0.35 alpha:1]]; 
    [day setTextAlignment:UITextAlignmentCenter]; 
    [day setBackgroundColor:[UIColor clearColor]]; 
    [day setClipsToBounds:YES]; 
    [viewRow addSubview:day]; 

    return viewRow; 
} 

- (NSString *)pickerView:(UIPickerView *)thePickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component { 
    return [arrayDays objectAtIndex:row]; 
} 

- (NSInteger)pickerView:(UIPickerView *)thePickerView numberOfRowsInComponent:(NSInteger)component { 
    return [arrayDays count]; 
} 
+0

对于UIPickerView的样式,请访问http://stackoverflow.com/questions/5744996/customizing-uipickerview-background-and-spacing – simonbs 2011-04-24 16:09:42