2015-04-02 107 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