[英]Change BG color of a view with tap recogniser
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. 我已经使用for循环创建了一个视图,如果单击一个视图,它的背景颜色应该会改变,而我已经实现了这一点,但是当我单击另一个视图时,以前的颜色将保持不变。
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 ? 我怎么解决这个问题 ?
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
. 您必须将上次更新的背景色重置为
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
andsetPurpleMediumColor
topurpleMediumColor
注意:我更正了拼写
selectedIndex
并将setPurpleMediumColor
为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循环中
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. 无论如何,只要像上面那样修改您的最后一个for循环即可。
Edit: 编辑:
You can remove the for-loop actually because that's no meaning. 您实际上可以删除for循环,因为那没有意义。
recognizer.view.backgroundColor = [UIColor setPurpleMediumColor];
if (self.lastSelectedView)
self.lastSelectedView.backgroundColor = [UIColor clearColor];
self.lastSelectedView = recognizer.view;
so you code will look like this. 因此您的代码将如下所示。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.