xcode 从视图中删除一些子视图

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4136733/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 03:35:26  来源:igfitidea点击:

xcode Removing Some Subviews from view

xcodesubviews

提问by S. Naughton

Greetings all,

问候大家,

I am a noob and I have been trying to work through this for a few days.

我是一个菜鸟,几天来我一直在努力解决这个问题。

I am adding images to a view via UItouch. The view contains a background on top of which the new images are add. How do I clear the images I am adding from the subview, without getting rid of the UIImage that is the background. Any assistance is greatly appreciated. Thanks in Advance.

我正在通过 UItouch 向视图添加图像。该视图包含在其上添加新图像的背景。如何清除我从子视图添加的图像,而不删除作为背景的 UIImage。非常感谢任何帮助。提前致谢。

here is the code:

这是代码:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *) event { 
NSUInteger numTaps = [[touches anyObject] tapCount];

if (numTaps==2) {
    imageCounter.text =@"two taps registered";      

//__ remove images  
    UIView* subview;
    while ((subview = [[self.view subviews] lastObject]) != nil)
        [subview removeFromSuperview];

    return;

}else {

    UITouch *touch = [touches anyObject];
    CGPoint touchPoint = [touch locationInView:self.view];
    CGRect myImageRect = CGRectMake((touchPoint.x -40), (touchPoint.y -45), 80.0f, 90.0f); 
    UIImageView *myImage = [[UIImageView alloc] initWithFrame:myImageRect];

    [myImage setImage:[UIImage imageNamed:@"pg6_dog_button.png"]];
     myImage.opaque = YES; // explicitly opaque for performance


    [self.view addSubview:myImage];
    [myImage release];

    [imagesArray addObject:myImage];

    NSNumber *arrayCount =[self.view.subviews count];
    viewArrayCount.text =[NSString stringWithFormat:@"%d",arrayCount];
    imageCount=imageCount++;
    imageCounter.text =[NSString stringWithFormat:@"%d",imageCount];

}

}

}

回答by James Huddleston

What you need is a way of distinguishing the added UIImageViewobjects from the background UIImageView. There are two ways I can think of to do this.

您需要的是一种将添加的UIImageView对象与背景区分开来的方法UIImageView。我可以想到两种方法来做到这一点。

Approach 1: Assign added UIImageViewobjects a special tag value

方法 1:为添加的UIImageView对象分配一个特殊的标签值

Each UIViewobject has a tagproperty which is simply an integer value that can be used to identify that view. You could set the tag value of each added view to 7 like this:

每个UIView对象都有一个tag属性,它只是一个可用于标识该视图的整数值。您可以像这样将每个添加的视图的标签值设置为 7:

myImage.tag = 7;

Then, to remove the added views, you could step through all of the subviews and only remove the ones with a tag value of 7:

然后,要删除添加的视图,您可以单步执行所有子视图,只删除标签值为 7 的视图:

for (UIView *subview in [self.view subviews]) {
    if (subview.tag == 7) {
        [subview removeFromSuperview];
    }
}

Approach 2: Remember the background view

方法二:记住背景视图

Another approach is to keep a reference to the background view so you can distinguish it from the added views. Make an IBOutletfor the background UIImageViewand assign it the usual way in Interface Builder. Then, before removing a subview, just make sure it's not the background view.

另一种方法是保留对背景视图的引用,以便您可以将其与添加的视图区分开来。IBOutlet为背景制作一个UIImageView并在界面生成器中以通常的方式分配它。然后,在删除子视图之前,只需确保它不是背景视图。

for (UIView *subview in [self.view subviews]) {
    if (subview != self.backgroundImageView) {
        [subview removeFromSuperview];
    }
}

回答by Sébastien REMY

A more swiftly code for approach #1 in only one functional line of code :

仅在一行功能代码中为方法 #1 提供更快速的代码:

self.view.subviews.filter({##代码##.tag == 7}).forEach({##代码##.removeFromSuperview()})