UITableViewCell中的UIView动画

时间:2015-04-02 03:38:59

标签: ios objective-c uitableview animation

我正在开发一个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;

                    }];

我在tableView:cellForRowAtIndexPath:的{​​{1}}数据源方法中调用的“设置内容”方法。

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

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

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

感谢。

3 个答案:

答案 0 :(得分:15)

我认为你应该再实施一个delegate UITableView方法,这是 -

- (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];
         }];
    }
}

答案 1 :(得分:-1)

swift 4 一样,您可以使用此动画

func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
        cell.alpha = 0
        cell.layer.transform = CATransform3DMakeScale(0.5, 0.5, 0.5)
        UIView.animate(withDuration: 0.4) {
            cell.alpha = 1
            cell.layer.transform = CATransform3DScale(CATransform3DIdentity, 1, 1, 1)
        }
    }

答案 2 :(得分:-1)

使用UIView动画。在Swift 4中可用。

func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
        cell.alpha = 0
        UIView.animate(withDuration: 0.3, animations: {
            cell.alpha = 1
        })
    }
相关问题