2011-03-12 84 views
1

我試圖實現一個動畫,當用戶點擊我的一個tableview單元格時發生。基本上,動畫只是一個帶有「+5」或「+1」這樣文本的小標籤,然後在衰落時向上移動(基本上就像用戶評分時在視頻遊戲中出現的點一樣)。動畫正在進行時無法與UITableView交互

tableView:didSelectRowAtIndexPath:實現我的控制器,我做以下(意譯爲簡單起見,這裏):

CGRect toastFrame = /* figure out the frame from the cell frame here */; 
UILabel *toast = [[UILabel alloc] initWithFrame:toastFrame]; 
toast.text = [NSString stringWithFormat:@"+%d", 5]; 
toast.backgroundColor = [UIColor clearColor]; 
toast.userInteractionEnabled = NO; // hoped this would work but it doesn't 
[tableView addSubview:toast]; 

[UIView 
animateWithDuration:1.0 
animations:^ 
{ 
    toast.alpha = 0.0; 
    toast.transform = CGAffineTransformMakeTranslation(0.0, -44.0); 
} 
completion:^ (BOOL finished) 
{ 
    [toast removeFromSuperview]; 
}]; 

[toast release]; 

的吐司是很好的顯現和看上去很不錯。問題是,直到動畫完成,tableview停止接收觸摸事件。這意味着在點擊tableview中的單元格後,您不能點擊tableview中的任何其他單元格。

有沒有辦法阻止這種情況發生,並允許用戶繼續與tableview交互,就好像動畫沒有發生一樣?

預先感謝任何與此幫助。

回答

5

另一個選擇可能是使用animateWithDuration:delay:options:animations:completion:(未經測試,從我的頭頂)

看看在options參數和可能的標記,通過UIViewAnimationOptions定義。其中包括一個名爲UIViewAnimationOptionAllowUserInteraction的標誌。這可能是一個解決方案,也許你應該嘗試一下!

+0

輝煌!那樣做了。關於如何以塊的方式實現它的好處是塊會自動保留來自外部的視圖,因此您無需擔心在調用完成處理程序之前手動確保臨時視圖未被銷燬。也給你做正式推薦的方式可疑的滿意度:) – glenc 2011-03-13 16:00:04

+0

五年後,你仍然保存我的一天。謝謝! – 2016-07-11 08:09:01

1

你試過用另一種方式做它嗎?例如,添加吐司作爲子視圖後,你可以做這樣的事情:

[UIView beginAnimations:nil context:NULL]; 
[UIView setAnimationCurve: UIViewAnimationCurveEaseOut]; 
[UIView setAnimationDuration: 1.0]; 
toast.alpha = 0.0; 
toast.frame.origin.y -= 44.0; 
[toast performSelector:@selector(removeFromSuperview) withObject: nil afterDelay: 1.0]; 
[UIView commitAnimations]; 

然後釋放吐司。你可以嘗試這種方式,看看它是否有效。

+0

嗨 - 非常感謝這個,這實際上起作用,並給了我所需的行爲。我想知道爲什麼塊動畫方法不起作用,因爲這是現在推薦的做事方式。 – glenc 2011-03-12 17:11:43

+0

真的,現在是推薦的方式嗎?我能想到的唯一原因是爲什麼它不能用於block方法,因爲block方法不會創建新線程,而此方法創建一個新線程,並且所有工作都在另一個線程中完成。也許他們將此設置爲默認行爲,以節省資源,因爲大多數人不需要在動畫過程中啓用交互,對於那些少數人,您可以使用此方法。只是我的猜測。 – Omar 2011-03-12 23:54:44

+0

根據[UIView beginAnimations:context:]的文檔,不再推薦這種方法(假設您的應用程序至少需要iOS 4.0):http://developer.apple.com/library/ios/ #documentation/uikit/reference/UIView_Class/UIView/UIView.html#// apple_ref/doc/uid/TP40006816-CH3-SW14 – glenc 2011-03-13 16:02:47