ios - Change BG color of a view with tap recogniser


Keywords:ios 


Question: 

I've created a view using for loop and if I click on a view ,Its background Color should change and Ive Achieved this, but when I click on another view the previous color remains the same.

my code

    _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];
    }

}

}

Achieved : While clicking the view Bg Color Change. Problem : While clicking another View the Previous Bg Color should reset to the Original Color.

How Can I Solve this Problem ?


3 Answers: 

use this else condition

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

You have to reset last updated background color to clearColor.

You can achieve this by,

    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];
    }

Note: I corrected spelling selectedIndex and setPurpleMediumColor to purpleMediumColor

 

This is so easy!

Just create an instance to store your last selected view.

@property UIView *lastSelectedView;

And store it at your last for-loop

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

Just modify your last for-loop as above anyway.


Edit:

You can remove the for-loop actually because that's no meaning.

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

so you code will look like this.