xcode Removing Some Subviews from view

淺唱寂寞╮ 提交于 2019-12-18 12:23:46

问题


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

}

}


回答1:


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



回答2:


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

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


来源:https://stackoverflow.com/questions/4136733/xcode-removing-some-subviews-from-view

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!