【发布时间】: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