为表视图单元格设置动画UIImageView

时间:2014-05-15 14:09:34

标签: ios objective-c uiimageview uiviewanimation

我正在尝试在"表格视图单元格"中设置UIImageview动画,但图像从不显示。 这是我试图使用的代码。

   - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
        cell.textLabel.font = [UIFont systemFontOfSize:16.0];
    }

    if (cell)
    {
        cell.textLabel.text = [titleArray objectAtIndex:indexPath.row];
        cell.textLabel.textColor = [UIColor darkGrayColor];

        UIImage *musicOne = [UIImage imageNamed:@"music1.png"];
        UIImage *musicTwo = [UIImage imageNamed:@"music2.png"];
        UIImage *musicThree = [UIImage imageNamed:@"music3.png"];

        NSArray *imagesArray = [NSArray arrayWithObjects:musicOne, musicTwo, musicThree, nil];

        if (indexPath.row == 0)
        {
            cell.imageView.animationImages = imagesArray;
            cell.imageView.animationDuration = 1.0f;
            cell.imageView.animationRepeatCount = 0;
            [cell.imageView startAnimating];
        }
    }

    return cell;
}

如果我没有尝试使用多张图片制作动画,并且只是在图片视图中使用一张图片,则图片会显示在单元格中。

cell.imageView.image = [imagesArray objectAtIndex:0];

3 个答案:

答案 0 :(得分:3)

有必要将image的{​​{1}}属性与cell.imageView一起设置,否则animationImages将不显示任何内容。

为了提高应用程序的性能并节省内存,请执行以下操作:

UITableViewCell

答案 1 :(得分:0)

尝试给imageView一个标签(例如1),然后得到如下图像视图:

    UIImage *musicOne = [UIImage imageNamed:@"music1.png"];
    UIImage *musicTwo = [UIImage imageNamed:@"music2.png"];
    UIImage *musicThree = [UIImage imageNamed:@"music3.png"];

    NSArray *imagesArray = [NSArray arrayWithObjects:musicOne, musicTwo, musicThree, nil];

    if (indexPath.row == 0)
    {
        UIImageView *imageView = (UIImageView *)[cell viewWithTag:1];
        imageView.animationImages = imagesArray;
        imageView.animationDuration = 1.0f;
        imageView.animationRepeatCount = 0;
        [imageView startAnimating];
    }

答案 2 :(得分:0)

这个完全相同的代码对我有用。我知道UIScrollView动画会在视图滚动时停止播放,因此您应该使用CADisplayLinkNSRunLoopCommonModes而不是NSDefaultRunLoopMode上运行动画,但我已经测试过你的代码,它工作。

相关问题