2013-05-10 92 views
4

我做了一个自定义的UIPickerView,但我想,当滚动到这样选定行的UILabel变色;UIPickerView所选行标签颜色

UIPickerView

任何想法?

编辑: 我想要做的是改变的UILabel 的颜色而选择正在取得,即同时车轮转向,而不是事后。

这里就是我这么远,这会改变的UILabel的颜色,你已经做出了选择后:

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view 
{ 
    AILabel * pickerRow = view ? (AILabel *)view:[[[AILabel alloc] initWithFrame:CGRectMake(0, 0, 140, 40)] autorelease]; 
    pickerRow.backgroundColor = [UIColor clearColor]; 
    pickerRow.font = [UIFont boldSystemFontOfSize:18.0f]; 
    pickerRow.insets = UIEdgeInsetsMake(0, 10, 0, 0); 

    if(component == 0) 
    { 
     pickerRow.text = [self.numberArray objectAtIndex:row]; 
     if (row == number) 
     { 
      pickerRow.alpha = 0.0f; 
      [UIView animateWithDuration: 0.33f 
            delay: 0.0f 
           options: UIViewAnimationOptionCurveEaseOut 
          animations:^{ 
           pickerRow.textColor = [UIColor whiteColor]; 
           pickerRow.shadowColor = [UIColor blackColor]; 
           pickerRow.shadowOffset = CGSizeMake(0.0f, 1.0f); 
           pickerRow.alpha = 1.0f; 
          } 
          completion:^(BOOL finished){ 
          }]; 
     } 
     else 
     { 
      pickerRow.textColor = [UIColor blackColor]; 
      pickerRow.shadowColor = [UIColor whiteColor]; 
      pickerRow.shadowOffset = CGSizeMake(0.0f, 1.0f); 
     } 
    } 
    else 
    { 
     pickerRow.text = [self.durationArray objectAtIndex:row]; 
     if (row == duration) 
     { 
      pickerRow.alpha = 0.0f; 
      [UIView animateWithDuration: 0.33f 
            delay: 0.0f 
           options: UIViewAnimationOptionCurveEaseOut 
          animations:^{ 
           pickerRow.textColor = [UIColor whiteColor]; 
           pickerRow.shadowColor = [UIColor blackColor]; 
           pickerRow.shadowOffset = CGSizeMake(0.0f, 1.0f); 
           pickerRow.alpha = 1.0f; 
          } 
          completion:^(BOOL finished){ 
          }]; 
     } 
     else 
     { 
      pickerRow.textColor = [UIColor blackColor]; 
      pickerRow.shadowColor = [UIColor whiteColor]; 
      pickerRow.shadowOffset = CGSizeMake(0.0f, 1.0f); 
     } 
    } 

    return pickerRow; 
} 

的AILabel只是一个自定义的UILabel,什么特别的地方。 “数字”变量是第一个组件中的当前选定值。 'duration'变量是第二个组件中的当前选定值。

希望这是更清晰

干杯

+0

http://stackoverflow.com/questions/7595220/how-to-change-color-selected-picked-value-from-pickerview – 2013-05-10 08:01:39

+0

内部消除任何代码都很复杂,以帮助您... – 2013-05-10 08:47:40

+0

粘贴您的代码,以便我们可以帮助您 – user247 2013-05-10 09:11:53

回答

-1
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view 
    { 
     UILabel *label = (UILabel*) view; 
     if (label == nil) { 
      label = [[UILabel alloc] init]; 
     } 

     label.textColor = [UIColor RedColor]; 
     CGSize rowSize = [pickerView rowSizeForComponent:component]; 
     CGRect labelRect = CGRectMake (0, 0, rowSize.width, rowSize.height); 
     [label setFrame:labelRect]; 

     return label; 
    } 



- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component 
{ 
UILabel *selectedRow = (UILabel *)[pickerView viewForRow:row forComponent:component]; 
     selectedRow.textColor = [UIColor blueColor]; 
}