iphone - 如何以编程方式绘制边框并在 UIImageview 周围的单词后将其删除?

标签 iphone ios uiview border

请先阅读我的问题,然后再将其标记为重复或重复问题...

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

  2. 当用户单击其中一个时,将显示下一个 View Controller 并且 单击图像的 uiview 得到绿色边框。

  3. 当用户导航回该 View 时,将显示点击的图像 带绿色边框。这一切都很好

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

编辑

enter image description here

经过一些来回导航

enter image description here

最佳答案

新答案- 我认为问题在于您一次又一次地添加新 View 。而不是执行 UIView *fr = [[UIView alloc] initWithFrame:frame];,找到已经存在的 View :UIView *fr = [self.scrollDisplay viewWithTag:index]; 并对其进行更改。实际上,新的 ImageView 被添加到旧的 ImageView 之上,除了导致上述问题之外,效率非常低。我假设您已经将 View 添加到 scrollDisplay。您也不应该再次创建新的 imgView 对象。为它们设置标签,使它们独一无二且易于获取。例如,为每个 imgView 设置标签 999 并将其检索为: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

关于iphone - 如何以编程方式绘制边框并在 UIImageview 周围的单词后将其删除?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10088128/

相关文章:

iphone - UIWebView底面

iphone - iOS : Move forward dynamically NSDate with button

ios - 从 iOS 中的 TextView 中删除单行

ios - Swift 中 UIScrollView 内的 UIPanGesture

ios - 如何在我自己的应用程序中实现 Apple Music 中的迷你播放器?

ios - 开源MDM服务器的选择

iphone - 在旧 iPhone/iPod Touch 上测试应用程序是否有意义

ios - 即使 Storyboard上没有 ADBannerView,iAd 横幅也会出现?

ios - 是否可以在 iOS 预览中为生成的 PDF 禁用 "Open In..."

ios - 如何将 UIImage View 和其中的文本保存到磁盘