简体   繁体   中英

How do I remove a UIImage View?

The code that I am using loads a sticker in the view. Instead of it being draggable, I'm just going to remove the previous UIImageView and create a new one each time there is a touch. This is the code that I am using, but it only keeps creating and doesn't delete the previous image view every frame. FYI I do not want to use GL, Sprites, or anything else. I want this to work.

In the viewDidLoad:

float x = 160, y = 100, w =50, h=50;

image0 =[[UIImageView alloc] initWithFrame:CGRectMake(x,y,w,h)];
image0.image=[UIImage imageNamed:@"cow-35561_150.png"];
[self.view addSubview:image0];

In touchesBegan:

image0 = nil;

UITouch *touch = [[event allTouches] anyObject];
CGPoint location = [touch locationInView:touch.view];

NSLog(@"%@", NSStringFromCGPoint(location));

image0 =[[UIImageView alloc] initWithFrame:CGRectMake(location.x,location.y,50,50)];
image0.image=[UIImage imageNamed:@"cow-35561_150.png"];

[self.view addSubview:image0];

To remove a view from the hierarchy, use removeFromSuperview

[image0 removeFromSuperview];
image0 = nil;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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