1

请在将其标记为重复或重复问题之前阅读我的问题...

  1. 我有一个 scoleView,我在其中以表格形式放置了一些图像。

  2. 当用户单击其中一个时,将显示下一个视图控制器,并且单击的图像的 uiview 获得绿色边框。

  3. 当用户导航回此视图时,单击的图像显示为绿色边框。这一切都很好

但是当用户单击其他图像时问题就开始了:先前单击的图像没有恢复正常,即即使我将其宽度设置为 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:方法中被调用

编辑

在此处输入图像描述

经过一些来回导航

在此处输入图像描述

4

2 回答 2

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
于 2012-04-10T13:14:34.190 回答
0

我只需要做的就是将边框颜色设置为视图的背景颜色。任何其他选项都没有做任何其他事情都没有隐藏边框我得到的唯一解决方法是这个

于 2012-04-25T08:32:32.950 回答