Question

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];
Was it helpful?

Solution

To remove a view from the hierarchy, use removeFromSuperview

[image0 removeFromSuperview];
image0 = nil;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top