4

我已经制作了一个自定义 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' 变量是第一个组件中的当前选定值。“持续时间”变量是第二个组件中的当前选定值。

希望这更清楚

干杯

4

1 回答 1

-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];
}
于 2017-01-27T07:44:19.157 回答