请在将其标记为重复或重复问题之前阅读我的问题...
我有一个scroleView,其中我以表格形式放置了一些图像。
当用户点击其中一个时,会显示下一个视图控制器 点击图片的uiview获得绿色边框。
但问题在用户点击其他图片时开始:先前点击的图片无法恢复正常,即,即使我将其宽度设为0.0并将颜色设置为clearColor
请指导我如何删除这些边框
我的代码如下:
for (int row = 0; row < r; ++row)
{
for (int col = 0; col < 2; ++col)
{
int index = (row * 2) + col;
if(index < [tempArr count])
{
CGRect frame = CGRectMake(10+col*(10+145),10+row*(5+100),145, 100);
UIView *fr = [[UIView alloc] initWithFrame:frame];
CGRect imgFrame = CGRectMake(0, 0, 145, 100);
UIImageView *imgView = [[UIImageView alloc]initWithFrame:imgFrame];
imgView.image = [UIImage imageNamed:[[tempArr objectAtIndex:index]valueForKey:@"add"]];
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];
fr.layer.borderWidth = 0.0f;
fr.layer.borderColor = [UIColor greenColor].CGColor;
if(selctedFrame == index)//Here i put border
{
fr.layer.borderWidth = 2.0f;
fr.layer.borderColor = [UIColor greenColor].CGColor;
}
else //here i remove them
{
fr.layer.borderWidth = 0.0f;
fr.layer.borderColor = [UIColor clearColor].CGColor;
}
tapGesture.numberOfTapsRequired = 1;
tapGesture.numberOfTouchesRequired = 1;
[fr addGestureRecognizer:tapGesture];
[fr addSubview:imgView];
fr.tag = index;
[self.scrollDisplay addSubview:fr];
[self.scrollDisplay bringSubviewToFront:fr];
}
}
}
[self.view addSubview:self.scrollDisplay];
此方法在viewWillAppear:animated:
方法
修改
经过一些导航后的
答案 0 :(得分:0)
新答案 - 我认为问题在于您一次又一次地添加新视图。不要执行UIView *fr = [[UIView alloc] initWithFrame:frame];
,而是将现有视图查找为:UIView *fr = [self.scrollDisplay viewWithTag:index];
并对其进行更改。实际上,除了引起上述问题之外,新的图像视图被添加到旧图像视图上,这是非常低效的。我假设您已经将视图添加到scrollDisplay
。您也不应该再次创建新的imgView
个对象。为它们设置标签,使它们独特且易于获取。例如,为每个999
设置标记imgView
并将其检索为:UIImageView *imgView = [fr viewWithTag:999];
此外,摆脱[fr addSubview:imgView];
,[self.scrollDisplay addSubview:fr];
和[self.view addSubview:self.scrollDisplay];
走向终点。所以你的代码应该是这样的:
for (int row = 0; row < r; ++row)
{
for (int col = 0; col < 2; ++col)
{
int index = (row * 2) + col;
if(index < [tempArr count])
{
CGRect frame = CGRectMake(10+col*(10+145),10+row*(5+100),145, 100);
UIView *fr = [self. scrollDisplay viewWithTag:index];
CGRect imgFrame = CGRectMake(0, 0, 145, 100);
UIImageView *imgView = [fr viewWithTag:999];
//imgView.image = [UIImage imageNamed:[[tempArr objectAtIndex:index]valueForKey:@"add"]]; //<--You've already set the image before so you don't need this
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];
fr.layer.borderWidth = 0.0f;
fr.layer.borderColor = [UIColor greenColor].CGColor;
if(selctedFrame == index)//Here i put border
{
fr.layer.borderWidth = 2.0f;
fr.layer.borderColor = [UIColor greenColor].CGColor;
}
else //here i remove them
{
fr.layer.borderWidth = 0.0f;
fr.layer.borderColor = [UIColor clearColor].CGColor;
}
tapGesture.numberOfTapsRequired = 1;
tapGesture.numberOfTouchesRequired = 1;
//[fr addGestureRecognizer:tapGesture]; //<-- Not needed if you've already done this
//[fr addSubview:imgView];//<-- Not needed
fr.tag = index;
//[self.scrollDisplay addSubview:fr];//<-- Not needed
[self.scrollDisplay bringSubviewToFront:fr];//<-- probably not needed
}
}
}
//[self.view addSubview:self.scrollDisplay];//<-- Not needed
答案 1 :(得分:0)
我只需要将边框颜色设置为视图的背景颜色。任何其他选项什么也没做什么边界没有通过任何其他东西隐藏我唯一的工作是这个