UIPickerView选定的行标签颜色

时间:2013-05-10 07:58:38

标签: iphone ios objective-c xcode uipickerview

我已经制作了一个自定义的UIPickerView,但我希望UILabel在滚动到这样的选定行时改变颜色;

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,没什么特别的。 'number'变量是第一个组件中当前选定的值。 'duration'变量是第二个组件中当前选定的值。

希望这更清楚

干杯

1 个答案:

答案 0 :(得分:-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];
}