2012-07-08 76 views
0

這是我第一次發表一些東西,首先你需要知道兩件事情,我非常熟悉objective-c,英語不是我的母語,所以如果你不明白以及請問:)乾淨的隨機圖像屏幕

好吧,我正在爲我的孩子做一個iPad應用程序,所以它包含隨機聲音和每個水龍頭上的隨機圖像。但是在點擊屏幕之後,屏幕會充滿圖像,所以我想知道如何在X拍攝後「清潔」屏幕。

一些代碼在這裏:

- (void)oneFingerOneTap{ 


int randomNumber = arc4random() % 9 + 1; 
int randX = arc4random() % 768 + 1; 
int randY = arc4random() % 1004 + 1; 

NSString *fileName = @"Sound"; 
NSString *ext = @".caf"; 
NSString *randString = [NSString stringWithFormat:@"%d", randomNumber]; 
NSString *completeSound = [NSString stringWithFormat:@"/%@%@%@", fileName, randString, ext]; 

NSString *path = [NSString stringWithFormat:@"%@%@", [[NSBundle mainBundle] resourcePath], completeSound]; 

SystemSoundID soundID; 
NSURL *filePath = [NSURL fileURLWithPath:path isDirectory:NO]; 


AudioServicesCreateSystemSoundID((__bridge CFURLRef)filePath, &soundID); 

AudioServicesPlaySystemSound(soundID); 


self.imgView = [[UIImageView alloc] initWithFrame:CGRectMake(randX, randY, 60, 60)]; 
self.imgView.image = [UIImage imageNamed:@"background.jpg"]; 
[self.view addSubview:self.imgView]; 




NSLog(@" %d", randX); 

count++; 
} 

所以我做一個全球INT(計數)知道水龍頭又做了多少事。

ps。我知道這個圖像在當時並不是隨機的,現在沒關係。

同樣,如何刪除之前完成的所有圖像?

我試過self.imgView.hidden = TRUE;但它只隱藏了最後一張圖片。

回答

0

試試這個代碼

for (UIView *subVw in self.view.subviews) { 
    if ([subVw isKindOfClass:[UIImageView class]]) { 
     [subVw removeFromSuperview]; 
    } 
} 
+0

哎!這工作正常!非常感謝,我不能投票,我需要更多的聲譽,我會給你,當我能夠:)謝謝兄弟 – 2012-07-08 07:36:18

+0

但removeAllSubviews不起作用,我能做什麼? – 2012-07-08 07:44:57

+0

removeAllSubviews將從您的屏幕中刪除視圖層次結構中的所有視圖,我記得使用該方法,現在我只注意到xcode在我嘗試調用該方法時抱怨(可能是SDK版本問題?不確定)。上面的代碼將刪除UIImageView的子視圖類型,所以如果你的屏幕上有UIButton等,它們將保留在屏幕上。您還可以將UIImageView的標記設置爲該UIImageView應該保留在屏幕上的標記,或者當您點按X時從屏幕上移除標記,以防止其他圖像出現在您不想刪除的位置。 – janusbalatbat 2012-07-08 08:00:22