2015-04-02 105 views
5

我正在開發一個iOS應用程序,在一個視圖中UITableView與兩個自定義UITableViewCell。所有這些使用主要故事板。

一切工作都很好。但我需要實現的是單個淡入/淡出動畫UIView只有一個 cell。

目前I'm使用這個代碼進行動畫顯示視圖:UIView動畫裏面的UITableViewCell

[UIView animateWithDuration:1.2 
         delay:0 
        options:UIViewAnimationOptionAutoreverse | UIViewAnimationOptionRepeat | UIViewAnimationOptionCurveEaseInOut animations:^{ 

         self.myView.alpha = 0; 

        } completion:^(BOOL finished) { 

         self.myView.alpha = 1; 

        }]; 

在「設定內容」的方法,我在UITableViewtableView:cellForRowAtIndexPath:數據源的方法調用。

動畫工作正常。即使視圖之間的變化仍然具有動畫效果。我的問題是,當我從表視圖或表視圖中重新加載數據時,向上或向下滾動直到帶有動畫的單元格消失,並且必須重新使用該單元格以再次顯示該單元格,此時單元格不顯示動畫。

我在這裏錯過了什麼?有沒有更好的方法來實現這一點?表格視圖重用單元格後,如何重新開始動畫?

我覺得很重要的是,根據我的UI設計,只有一個單元格會有動畫。

謝謝。

回答

12

我想你應該實現的一個UITableViewdelegate方法,這是 -

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath 

&在它裏面的各單元的各自的觀點進行您的動畫。

下面的方法都會被調用顯示單元的時間,(也是在情況下,如果它超出了TableView中的框架,並回歸到可視區域)

-(void) tableView:(UITableView *) tableView willDisplayCell:(UITableViewCell *) cell forRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    //row number on which you want to animate your view 
    //row number could be either 0 or 1 as you are creating two cells 
    //suppose you want to animate view on cell at 0 index 
    if(indexPath.row == 0) //check for the 0th index cell 
    { 
     // access the view which you want to animate from it's tag 
     UIView *myView = [cell.contentView viewWithTag:MY_VIEW_TAG]; 

     // apply animation on the accessed view 
     [UIView animateWithDuration:1.2 
         delay:0 
        options:UIViewAnimationOptionAutoreverse | UIViewAnimationOptionRepeat | UIViewAnimationOptionCurveEaseInOut animations:^ 
     { 
        [myView setAlpha:0.0]; 
     } completion:^(BOOL finished) 
     { 
        [myView setAlpha:1.0]; 
     }]; 
    } 
} 
+0

呀,THA幫了不少忙,謝謝!。但在其他一些情況下,如'reloadData'方法仍然不起作用。感謝您的迴應,並使用'tableView:didEndDisplayingCell:forRowAtIndexPath:'方法來停止動畫,因爲它似乎也會導致問題。 – 2015-04-02 05:54:43

+0

在willDisplayCell中:如果您將動畫視圖的時間設置爲1.2秒,那麼此動畫會自動停止,實際上我並不瞭解tableView:didEndDisplayingCell:forRowAtIndexPath:方法的用法。我需要知道它的用途,請你稍微提高一點,謝謝。 – 2015-04-02 06:08:49

+0

我使用它從視圖中移除動畫,因爲我意識到如果動畫是中斷,它將不會再次開始。 – 2015-04-02 15:20:00