简体   繁体   English

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

[英]how to programmatically draw border and remove it after words around UIImageview?

Please read my question before marking it as duplicate or repeat question...请在将其标记为重复或重复问题之前阅读我的问题...

  1. I have one scroleView in which I put some images in tabular form.我有一个 scroleView,其中我以表格形式放置了一些图像。

  2. When user click one of it the next view controller is shown and the clicked image's uiview get green border.当用户单击其中一个时,将显示下一个视图 controller,并且单击图像的 uiview 会出现绿色边框。

  3. And when user navigate back to this view the clicked image is shown with green border.当用户导航回该视图时,单击的图像显示为绿色边框。 all this worked fine这一切都很好

But the problem starts when user clicks other images: the previously clicked image doesn't get back to normal, ie, its border stays there even if I put its width to 0.0 and color to clearColor但是当用户单击其他图像时问题就开始了:之前单击的图像不会恢复正常,即即使我将其宽度设置为 0.0 并将颜色设置为clearColor ,它的边框仍然存在

Please guide me how to remove those borders请指导我如何删除这些边框

My code is as below:我的代码如下:

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];

This method is called in viewWillAppear:animated: method这个方法在viewWillAppear:animated:方法中被调用

Edit编辑

在此处输入图像描述

after some navigation forth and back经过一些来回导航

在此处输入图像描述

New Answer- I think the problem is that you're adding new views again and again.新答案-我认为问题在于您一次又一次地添加新视图。 Instead of doing UIView *fr = [[UIView alloc] initWithFrame:frame];而不是做UIView *fr = [[UIView alloc] initWithFrame:frame]; , find the already existing view as: UIView *fr = [self.scrollDisplay viewWithTag:index]; ,找到已经存在的视图为: UIView *fr = [self.scrollDisplay viewWithTag:index]; and make changes to it.并对其进行更改。 In effect, new imageviews are being added over the old ones which is highly inefficient, besides causing the mentioned issue.实际上,新的图像视图被添加到旧的图像视图之上,除了导致上述问题之外,效率非常低。 I'm assuming you've already added the views to scrollDisplay .我假设您已经将视图添加到scrollDisplay You also shouldn't create new imgView objects again.您也不应该再次创建新的imgView对象。 Set tags for them that make them unique and easy to get.为它们设置标签,使它们独一无二且易于获取。 For eg, set the tag 999 for each imgView and retrieve it as: UIImageView *imgView = [fr viewWithTag:999];例如,为每个imgView设置标签999并将其检索为: UIImageView *imgView = [fr viewWithTag:999]; Also, get rid of [fr addSubview:imgView];另外,摆脱[fr addSubview:imgView]; , [self.scrollDisplay addSubview:fr]; , [self.scrollDisplay addSubview:fr]; and [self.view addSubview:self.scrollDisplay];[self.view addSubview:self.scrollDisplay]; towards the end.接近尾声。 So your code should look like this:所以你的代码应该是这样的:

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

i just need to do is to set the border color to the background color of the view.我只需要将边框颜色设置为视图的背景颜色。 any other options did nothing the border is not been hidden through any thing else the only work around i got is this任何其他选项都没有任何作用边界没有被其他任何东西隐藏我得到的唯一解决方法是这个

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM