ios - tableview cell.imageview 没有动画

标签 ios uitableview imageview tableview

我正在尝试为默认存在的 tableview 单元格的 imageview 设置动画,我希望 ImageView 仅在单元格可见时设置一次动画,为此我编写了以下代码。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }

    NSArray *myImageArray = [NSArray arrayWithObjects:
                             [UIImage imageNamed:[NSString stringWithFormat:@"wave6.png"]],
                             [UIImage imageNamed:[NSString stringWithFormat:@"wave7.png"]],
                             [UIImage imageNamed:[NSString stringWithFormat:@"wave8.png"]],
                             [UIImage imageNamed:[NSString stringWithFormat:@"wave9.png"]],
                             [UIImage imageNamed:[NSString stringWithFormat:@"wave10.png"]],nil];

        NSArray *titles = @[@"John Appleseed", @"John Doe", @"Test User",@"John Appleseed", @"John Doe", @"Test User",@"John Appleseed", @"John Doe", @"Test User",
                            @"Test User",@"John Appleseed", @"John Doe", @"Test User",@"John Appleseed", @"John Doe", @"Test User",@"John Appleseed", @"John Doe", @"Test User",
                            @"Test User"];
    [cell.imageView setAnimationImages:myImageArray];
    cell.imageView.animationDuration = 4.20;
    cell.imageView.animationRepeatCount = 1;
    [cell.imageView startAnimating];


            cell.textLabel.text = titles[indexPath.row];

    return cell;
}

最佳答案

<强>1。去除cellforatindexpath中的动画代码

<强>2。在单元格显示时为其设置动画

<强>3。隐藏单元格时停止动画

<强>4。将提高应用程序的性能

<强>5。节省更多内存

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

        NSArray *myImageArray = [NSArray arrayWithObjects:
                                 [UIImage imageNamed:[NSString stringWithFormat:@"wave6.png"]],
                                 [UIImage imageNamed:[NSString stringWithFormat:@"wave7.png"]],
                                 [UIImage imageNamed:[NSString stringWithFormat:@"wave8.png"]],
                                 [UIImage imageNamed:[NSString stringWithFormat:@"wave9.png"]],
                                 [UIImage imageNamed:[NSString stringWithFormat:@"wave10.png"]],nil];

        [cell.imageView setAnimationImages:myImageArray];
        cell.imageView.animationDuration = 4.20;
        cell.imageView.animationRepeatCount = 1;
        [cell.imageView startAnimating];

    }

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


        [cell.imageView stopAnimating];

        [cell.layer removeAllAnimations];

    }

代替

 [NSString stringWithFormat:@"wave6.png"]

你可以像@"wave6.png"那样指定

关于ios - tableview cell.imageview 没有动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23558096/

相关文章:

java - android ImageView 内存不足错误

安卓,XML : Show only portion of image in layout container without altering aspect ratio

ios - 从 Apple 应用内购买收据获取订阅付款历史记录

uitableview - 如何删除 UITableView 部分之间的垂直间距?

ios - 伪造到另一个 View Controller 的导航

ios - 尝试为 UITableView 设置背景图片,但加载有延迟

arrays - 无法使用 [NSIndexPath] 类型的索引为 [String] 类型的值添加下标 -prepareForSegue

Android 在RelativeLayout 前面设置ImageView

ios - CoreMotion 在 iOS 10 上延迟传送数据

ios - 从导航堆栈中删除 viewController 时出现问题