2010-07-08 38 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