Objective c 如何删除UIImage视图?

Objective c 如何删除UIImage视图?,objective-c,uiimageview,Objective C,Uiimageview,我使用的代码在视图中加载了一个标签。与其说它是可拖动的,我只想删除以前的UIImageView,并在每次触摸时创建一个新的UIImageView。这是我正在使用的代码,但它只会不断创建,不会在每一帧删除上一个图像视图。仅供参考,我不想使用GL、精灵或其他任何东西。我希望这能奏效 在viewDidLoad中: float x = 160, y = 100, w =50, h=50; image0 =[[UIImageView alloc] initWithFrame:CGRectMake(x,y

我使用的代码在视图中加载了一个标签。与其说它是可拖动的,我只想删除以前的UIImageView,并在每次触摸时创建一个新的UIImageView。这是我正在使用的代码,但它只会不断创建,不会在每一帧删除上一个图像视图。仅供参考,我不想使用GL、精灵或其他任何东西。我希望这能奏效

在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];
接触开始时:

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

要从层次结构中删除视图,请使用
removeFromSuperview

[image0 removeFromSuperview];
image0 = nil;

要从层次结构中删除视图,请使用
removeFromSuperview

[image0 removeFromSuperview];
image0 = nil;

要从层次结构中删除视图,请使用
removeFromSuperview

[image0 removeFromSuperview];
image0 = nil;

要从层次结构中删除视图,请使用
removeFromSuperview

[image0 removeFromSuperview];
image0 = nil;

我也编辑了一些很好的代码。image0=[[UIImageView alloc]initWithFrame:CGRectMake(location.x-25,location.y-25,50,50)];这将允许触摸位于中间而不是左上角进行拖动。我也为此编辑了一些很好的代码。image0=[[UIImageView alloc]initWithFrame:CGRectMake(location.x-25,location.y-25,50,50)];这将允许触摸位于中间而不是左上角进行拖动。我也为此编辑了一些很好的代码。image0=[[UIImageView alloc]initWithFrame:CGRectMake(location.x-25,location.y-25,50,50)];这将允许触摸位于中间而不是左上角进行拖动。我也为此编辑了一些很好的代码。image0=[[UIImageView alloc]initWithFrame:CGRectMake(location.x-25,location.y-25,50,50)];这将允许在中间而不是左上角拖动触摸。