使用点击识别器更改视图的BG颜色

时间:2017-06-09 07:34:15

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

我使用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];
    }

}

}

已实现:单击视图Bg颜色更改时。 问题:单击另一个时查看上一个Bg颜色应重置为原始颜色。

我如何解决这个问题?

3 个答案:

答案 0 :(得分:0)

使用其他条件

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

答案 1 :(得分:0)

您必须将上次更新的背景颜色重置为bash -c "find /Users/ihainan/IdeaProjects/ScalaForTheImpatient -name *.class"

您可以通过

实现这一目标
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]; } selectedIndex   到setPurpleMediumColor

答案 2 :(得分:0)

这很容易!

只需创建一个实例即可存储您上次选择的视图。

@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;

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