简体   繁体   English

如何删除UIImage视图?

[英]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. 与其拖动起来,不如将其删除,而是每次触摸时都删除以前的UIImageView并创建一个新的UIImageView。 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. 仅供参考,我不想使用GL,Sprites或其他任何东西。 I want this to work. 我要这个工作。

In the viewDidLoad: 在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 要从层次结构中删除视图,请使用removeFromSuperview

[image0 removeFromSuperview];
image0 = nil;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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