2010-07-08 37 views
0

笏我试图是,每次用户触摸屏幕,我是在屏幕添加图像在运行时阵列和清除它按钮点击

UIImage *img = [UIImage imageNamed:@"shoot_a.png"]; //bullet shot image 
     im.image = img; 
[self.view addSubview:im]; 

增加一个小bulletr图像时,如果用户触摸的50倍,屏幕上会显示50张图片。

现在我想,如果用户点击一个按钮,所有这些子弹应该从屏幕上删除。

我希望我明确我的问题。

[im removefromsuperview]不适合我。

如果我想在运行时将图像添加到数组,我该如何添加和释放? 还是有没有更好的方法来清除所有图像 问候。


这是我的联系方式,即时通讯将在屏幕上FRM heere子弹图片,,但这些图像没有在阵

// Touch method 
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 
    if(restrictTap == YES){ 



    NSUInteger numTaps = [[touches anyObject] tapCount]; 
    NSString *numTapsMessage = [[NSString alloc] initWithFormat:@"%i", numTaps]; 
     [numTapsMessage release]; 
    // getting location 
    CGPoint touchLocation = [[touches anyObject] locationInView:self.view]; 
    NSString *locationMessage = [[NSString alloc] initWithFormat:@"Location: X = %.0f Y = %.0f", touchLocation.x, touchLocation.y]; 
     //bullet shot image 
     im = [[UIImageView alloc] initWithFrame:CGRectMake(self.location.x, self.location.y, 12.0, 12.0)]; 

     float j =self.location.x; 
     float i =self.location.y; 

     img = [UIImage imageNamed:@"shoot_a.png"]; //bullet shot image 
     im.image = img; 
     //im.hidden = NO; 
     [self.view addSubview:im]; ///bullet shot image is over transparent view    

     [imageArray addObject:img]; 



} 
} 

回答

1

获取添加你可以通过NSMutableArray财产跟踪它们的和用它来删除它们:

// create e.g. in initializer: 
imageArray = [[NSMutableArray alloc] init]; 

// clean-up, e.g. in dealloc: 
self.imageArray = nil; 

// add image: 
[imageArray addObject:im]; 

// remove all: 
for (UIView *img in imageArray) { 
    [img removeFromSuperview]; 
} 
[imageArray removeAllObjects]; 
+0

@ this [imageArray addObject:im]; 我每次都得到0个对象.. wat做什么? – iscavengers 2010-07-08 05:19:27

+0

@shi:你是什么意思?数组保持空白?现在有两件事:你有'addObject:img'而不是'addObject:im',并且图像视图被过度保留。 – 2010-07-08 06:52:22

+0

@georg yes array is emmmmpty,没有图像被添加到数组中。 它显示0个对象 好吧,让我试试img im的instread。 – iscavengers 2010-07-08 06:58:20

1

可以在特定的标签设置为所有的子弹imageViews当你将它添加到您的视图

im.tag = 1000; 

然后在按钮触摸上,您可以遍历视图的子视图数组,并使用该标记删除所有视图。像这样:

int count = [[self.view subviews] count]; 

for(int i = 0; i < count; i++) 
{ 
    if([(UIView*)[[self.view subviews] objectAtIndex:i] tag] == 1000) 
    { 
     [(UIView*)[[self.view subviews] objectAtIndex:i] removeFromSuperview] 
    } 
} 
+0

谢谢lukya ... 我无法得到它的工作...... 可以操纵我的代码,我评论作为答案 问候 – iscavengers 2010-07-08 06:43:12