【问题标题】:Change BG color of a view with tap recogniser使用点击识别器更改视图的背景颜色
【发布时间】:2017-11-11 03:00:46
【问题描述】:

我已经使用 for 循环创建了一个视图,如果我点击一个视图,它的背景颜色应该会改变并且我已经实现了这一点,但是当我点击另一个视图时,之前的颜色保持不变。

我的代码

    _dropDownView = [[UIView alloc]initWithFrame:CGRectMake(SCREEN_WIDTH/3.5, -SCREEN_WIDTH, SCREEN_WIDTH/2+33, SCREEN_WIDTH - 100)];

for(int index = 0 ; index < indexCount ; index++)
{
    _dropDownViewCell = [[UIView alloc] initWithFrame:CGRectMake(0, index * dropCellHeight, SCREEN_WIDTH, dropCellHeight)];

    //Set Tag for future identification
    [_dropDownViewCell setTag:index];
    [_dropDownViewCell setBackgroundColor:[UIColor clearColor]];

    [_dropDownView addSubview:_dropDownViewCell];

    selectDropCell =
    [[UITapGestureRecognizer alloc] initWithTarget:self
                                            action:@selector(dropCellAction:)];

    [_dropDownViewCell addGestureRecognizer:selectDropCell];

}
- (void)dropCellAction:(UITapGestureRecognizer *)recognizer
{

switch (recognizer.view.tag) {

    case 0: {
        selectDropCell.view.tag = 0;

    }
        break;
    case 1: {
        selectDropCell.view.tag = 1;

    }
        break;
    case 2: {
        selectDropCell.view.tag = 2;

    }
        break;
    case 3: {

        selectDropCell.view.tag = 3;

    }
        break;
    case 4: {
        selectDropCell.view.tag = 4;


    }
        break;
    case 5: {

        selectDropCell.view.tag = 5;

    }
        break;
    case 6: {

        selectDropCell.view.tag = 6;

    }
        break;

    default:
        break;
}

for(int index = 0 ; index < 6 ; index ++){


    NSUInteger slectedIndex = selectDropCell.view.tag;
    if(slectedIndex == index)
    {
        recognizer.view.backgroundColor = [UIColor setPurpleMediumColor];
    }

}

}

已实现:单击视图背景颜色更改时。 问题:单击另一个视图时,以前的背景颜色应重置为原始颜色。

我该如何解决这个问题?

【问题讨论】:

    标签: ios objective-c view background-color uitapgesturerecognizer


    【解决方案1】:

    使用这个 else 条件

    if(slectedIndex == index)
       {
         recognizer.view.backgroundColor = [UIColor setPurpleMediumColor]; 
        } 
        else 
        {
         recognizer.view.backgroundColor = [UIColor clearColor];//or your desired color 
        }
    

    【讨论】:

    • 这不适用,因为recognizer.view 仅保留选定的视图。因此,您的代码最后使选定的视图变为透明颜色。所以视觉上什么都没有发生。
    【解决方案2】:

    您必须将上次更新的背景颜色重置为clearColor

    你可以通过,

        NSUInteger selectedIndex = selectDropCell.view.tag;
        UIView *view = [self.view viewWithTag: index]; //self.view means _dropDownViewCell's view (parent view)
        if(selectedIndex == index)
        {
            view.backgroundColor = [UIColor purpleMediumColor];
        }
        else
        {
            view.backgroundColor = [UIColor clearColor];
        }
    

    注意:我更正了拼写 selectedIndexsetPurpleMediumColorpurpleMediumColor

    【讨论】:

    • 这不适用,因为recognizer.view 仅保存选定的视图。因此,您的代码最后使选定的视图变为透明颜色。所以视觉上什么都没有发生。
    【解决方案3】:

    这太容易了!

    只需创建一个实例来存储您最后选择的视图。

    @property UIView *lastSelectedView;
    

    并将其存储在最后一个 for 循环中

    for(int index = 0 ; index < 6 ; index ++){
        NSUInteger slectedIndex = selectDropCell.view.tag;
        if(slectedIndex == index)
        {
            recognizer.view.backgroundColor = [UIColor setPurpleMediumColor];
            if (self.lastSelectedView) 
                self.lastSelectedView.backgroundColor = [UIColor clearColor];
            self.lastSelectedView = recognizer.view;
        }
    }
    

    无论如何,只要像上面那样修改你的最后一个 for 循环。


    编辑:

    实际上你可以删除 for 循环,因为那没有意义。

    recognizer.view.backgroundColor = [UIColor setPurpleMediumColor];
    if (self.lastSelectedView) 
        self.lastSelectedView.backgroundColor = [UIColor clearColor];
    self.lastSelectedView = recognizer.view;
    

    所以你的代码看起来像这样。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-11
      • 2015-08-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多