iOSAaronDavid
iOSAaronDavid

Reputation: 140

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

Upvotes: 0

Views: 1444

Answers (1)

mspensieri
mspensieri

Reputation: 3521

To remove a view from the hierarchy, use removeFromSuperview

[image0 removeFromSuperview];
image0 = nil;

Upvotes: 3

Related Questions