UITableViewCell中的UIView动画

Fer*_*oso 8 animation objective-c uitableview ios

我正在开发一个iOS应用程序,在一个视图中UITableView有两个自定义UITableViewCell.所有这些都使用了主要的故事板.

一切都很好.但我需要实现一个单一的淡入/在一个淡出动画UIView只有一个细胞.

目前我正在使用此代码为视图设置动画:

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

                        self.myView.alpha = 0;

                    } completion:^(BOOL finished) {

                        self.myView.alpha = 1;

                    }];
Run Code Online (Sandbox Code Playgroud)

在一个"设置内容"方法,我在tableView:cellForRowAtIndexPath:数据源方法中调用UITableView.

动画效果还可以.即使视图之间的变化,视图仍然是动画.我的问题是当我从表视图重新加载数据或表视图向上或向下滚动,直到带有动画的单元格消失,并且它必须重新使用单元格再次显示它,在那一刻,单元格没有动画.

我在这里想念的是什么?有没有更好的方法来实现这个?在表视图重用单元格后如何重新启动动画?

我认为重要的是要注意,根据我的UI设计,只有一个单元格会有动画.

谢谢.

San*_*ani 15

我认为你应该再实现一种delegate方法UITableView,即 -

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
Run Code Online (Sandbox Code Playgroud)

并在其中相应单元格的相应视图上执行动画.

每次显示单元格时都会调用以下方法(如果它离开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];
         }];
    }
}
Run Code Online (Sandbox Code Playgroud)