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

发布于 2024-10-01 23:51:21 字数 1285 浏览 8 评论 0原文

大家好,

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

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

这是代码:

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

}

}

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.

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

}

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

德意的啸 2024-10-08 23:51:21

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

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

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

myImage.tag = 7;

然后,要删除添加的视图,您可以逐步遍历所有子视图,仅删除标签值为 7 的子视图:

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

方法 2:记住背景视图

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

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

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

Approach 1: Assign added UIImageView objects a special tag value

Each UIView object has a tag property 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:

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:

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 IBOutlet for the background UIImageView and assign it the usual way in Interface Builder. Then, before removing a subview, just make sure it's not the background view.

for (UIView *subview in [self.view subviews]) {
    if (subview != self.backgroundImageView) {
        [subview removeFromSuperview];
    }
}
北方的韩爷 2024-10-08 23:51:21

仅用一行功能代码即可更快速地编写方法#1:

self.view.subviews.filter({$0.tag == 7}).forEach({$0.removeFromSuperview()})

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

self.view.subviews.filter({$0.tag == 7}).forEach({$0.removeFromSuperview()})
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文