2014-02-16 57 views
0

我希望當我點擊圖像時,另一個圖像出現1或2秒。當我點擊圖像時動畫與圖像

我有兩個圖像(UIImage),一個用於按下,另一個用於未按下。

是這樣的嗎? :

[UIView animateWithDuration: 0.2 delay: 0.0 options: UIViewAnimationOptionAutoreverse animations:^{ 
    pressed.alpha = 1.0; 
    unPressed.alpha = 0.0; 
}completion:^(BOOL finished){ 
    pressed.alpha = 0.0; 
    unPressed.alpha = 1.0; 
}]; 

但是,我看不到按下的圖像。

編輯:我的問題是ASIGN圖像到UIImageView的,我認爲,因爲當我點擊它出現擠壓的圖像,但隨後未按圖像不會出現

我想任何類似:

[UIView animateWithDuration: 2.0 delay: 0.0 options: UIViewAnimationOptionAutoreverse animations:^{ 
    pressed.alpha = 1.0; 
    unPressed.alpha = 0.0; 
    self.imageButton.image = pressed.image; 
}completion:^(BOOL finished){ 
    pressed.alpha = 0.0; 
    unPressed.alpha = 1.0; 
    self.imageButton.image = unPressed.image; 
}]; 
+0

您已將動畫持續時間設置爲.2,因此在完成前它不會顯示很長時間。如果你想要圖像出現2秒,請將animateWithDuration更改爲2. –

+1

編輯完成後:第二個代碼存在很多問題,但是你想要做什麼?只需讓按鈕圖像顯示2秒鐘,或者您想讓它淡入? –

+0

我想讓它在點擊它時像一個按鈕一樣淡入2秒 – user1911

回答

0

兩個圖像,但三個變量。每次運行此功能時,您都會看到unPressed圖像+按下的圖像(您將看到稍後添加到視圖中的圖像,因爲它會在前面)。

0

可能按下的圖像是否與正常重疊?嘗試以下操作:

[UIView animateWithDuration: 0.2 delay: 0.0 options: UIViewAnimationOptionAutoreverse animations:^{ 
    pressed.alpha = 1.0; 
    unPressed.alpha = 0.0; 
}completion:^(BOOL finished){ 
    pulsando.alpha = 0.0; 
    unPressed.alpha = 1.0; 
    pressed.alpha = 0.0; 
}]; 
1

首先,你設置動畫長到0.2,所以你可能要增加持續時間1.0或2.0,如果你實際上希望它顯示1或2秒。

其次,我假設這些圖像相互重疊,所以當兩個alpha都是1時,仍然只會顯示其中一個圖像。確保unPressed在開始動畫前隱藏;那麼確保在結束動畫後隱藏pressed

unPressed.alpha = 0; 

[UIView animateWithDuration: 2.0 delay: 0.0 options: UIViewAnimationOptionAutoreverse animations:^{ 
    pressed.alpha = 1.0; 
} completion:^(BOOL finished){ 
    pulsando.alpha = 0.0; 
    pressed.alpha = 0.0; 
    unPressed.alpha = 1.0; 
}]; 

(以及你一個問題。你說你想要的形象出現1或2秒。您想要的形象纔出現?還是你希望它消失的?只是雙重檢查......因爲如果你只是希望它的出現,你不需要動畫塊)

編輯:(在回答您的編輯)

好像你並不需要一個動畫塊在所有而你只是想讓圖像顯示2秒鐘。在這種情況下,當按下按鈕時,顯示圖像,然後在2秒後隱藏圖像。例如:

- (IBAction)buttonSelected:(UIButton*)sender { 
    pressed.alpha = 1.0; 
    unPressed.alpha = 0.0; 
    [self performSelector:@selector(hidePressedImage) withObject:nil afterDelay:2.0]; 
} 

- (void)hidePressedImage { 
    pressed.alpha = 0.0; 
    unPressed.alpha = 1.0; 
} 
+0

我的問題是將圖像設爲UIImageView我認爲,因爲當我點擊它時,按下的圖像出現,但然後unPressed圖像不出現。 – user1911

+0

但是unPressed圖像本來是出現了嗎? –

+0

如果是這樣,確保在我的答案中寫完後隱藏按下的圖像,因爲如果最初出現unPressed圖像,則會正確分配UIImageView;但是,在動畫之後,按下的圖像會隱藏它。 –