2012-03-12 128 views
0

我想要實現的是:在視圖上顯示圖像1秒

當我觸摸按鈕時,圖像在視圖上顯示1秒,然後圖像消失。

我知道NSTimer會幫助,但我不知道如何編寫正確的代碼...需要您的幫助,謝謝。

- (IBAction)bodytouched:(id)sender { 
bodytouchedimage.hidden = NO; 
    bodytouchedimage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"beated.jpg"]]; 
    bodytouchedimage.userInteractionEnabled = YES; 
    timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(showPictures:) userInfo:nil repeats:NO]; 
} 


- (void)showPictures:(NSTimer *)timer {  
bodytouchedimage.hidden = YES; 
} 
+0

什麼不有關工作?它看起來應該起作用。這不適合你嗎? – mattjgalloway 2012-03-12 20:43:33

+0

現在,它的工作。我在.xib文件的視圖中放置了UIImageView,進行連接,爲它選擇了一個圖像,並刪除了第3行的代碼。 – iPhrog 2012-03-12 21:14:53

回答

3

你應該到的是調用showPictures功能,當你觸摸按鈕,然後在showPictures方法中添加一個NSTimer,將調用一個方法hidePictures過1秒

- (void)showPictures 
{ 
    bodytouchedimage.hidden = NO; 
    [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(hidePictures) userInfo:nil repeats:NO]; 
} 

- (void)hidePictures 
{ 
    bodytouchedimage.hidden = YES; 
} 
+1

是啊!這正是我終於做到的。謝謝 – iPhrog 2012-03-12 21:15:58

1

比使用的NSTimer相反,它會簡單地調用你的方法來隱藏圖像像這樣簡單:

- (IBAction)bodytouched:(id)sender { 
bodytouchedimage.hidden = NO; 
    bodytouchedimage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"beated.jpg"]]; 
    bodytouchedimage.userInteractionEnabled = YES; 
    [self performSelector:@selector(hidePicture) withObject:nil afterDelay:1]; 
} 


- (void)hidePicture {  
bodytouchedimage.hidden = YES; 
} 

performSelector:withObject:afterDelay:是NSObject類的方法。

+0

It works!thank you!順便說一句,你在@selector後錯過了「:」 (hidePicture :) – iPhrog 2012-03-12 21:07:15