UITableViewCell中的UIView动画 [英] UIView animation inside UITableViewCell

查看:122
本文介绍了UITableViewCell中的UIView动画的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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


一切都很好。但我需要实现的是从 只有一个 单元格中的一个 UIView 中的单个淡入/淡出动画。


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

I´m developing an iOS app that has in one view a UITableView with two custom UITableViewCell. All this using a main storyboard.

Everything works pretty fine. But what I need to implement is a single fade in/fade out animation in one UIView from just one cell.

Currently I´m using a this code to animate the view:

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

                        self.myView.alpha = 0;

                    } completion:^(BOOL finished) {

                        self.myView.alpha = 1;

                    }];

我在 tableView中调用的set content方法:cellForRowAtIndexPath : UITableView 的数据源方法。

In a "set content" method that I call in the tableView:cellForRowAtIndexPath: data source method of the UITableView.

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

The animations works ok. Even if a change between views the view still animating. My problem is when I reload the data from the table view or the table view scrolls up or down until the cell with the animation disappear and it has to reuse the cell to show it again, in that moment the cell is presented with no animation.

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

What am I missing in here? Is there a better way to implement this? How do I restart the animation after the table view reuses the cell?

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

I think is important to remark that according to my UI design only one cell will have the animation.

谢谢。

推荐答案

我认为你应该再实现一个委托方法 UITableView ,这是 -

I think you should implement one more delegate method of UITableView, which is -

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

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

& perform your animations on the respective view of the respective cell inside it.

每次显示单元格时都会调用以下方法(如果它出现的话也是如此) TableView的框架并回到可见区域)

The below method will be called every time the cell is displayed, (also in case if it goes out of the TableView's frame and comes back to the visible area)

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

这篇关于UITableViewCell中的UIView动画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆