简体   繁体   English

tableview cell.imageview没有动画

[英]tableview cell.imageview is not animating

I am trying to animate tableview cell's imageview which is present by default, i want the image view to be animated only once when the cell is visible, for this i have written the following code. 我正在尝试设置默认情况下存在的tableview单元的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. Remove the animation code in cellforatindexpath 1.删​​除cellforatindexpath中的动画代码

2. animate it when cell is displaying 2.在单元格显示时对其进行动画处理

3. stop animation while hiding the cells 3.在隐藏单元格的同时停止动画

4. Will Improve the performance of the App 4.将改善应用程序的性能

5. Save more memory 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];

    }

instead of 代替

 [NSString stringWithFormat:@"wave6.png"]

you could specify just like @"wave6.png" 您可以像@“ wave6.png”一样指定

Create object of UIImageView and add on cell contentView 创建UIImageView的对象并添加单元格contentView

UIImageView *imageView = [[UIImageView alloc] initWithFrame:imageFrame];
imageView.animationImages = myImageArray;
imageView.animationDuration = 4.20;
imageView.animationRepeatCount = 1;
imageView.image = [UIImage imageNamed:@"wave5.png"]
    ; // you can put any image
[imageView startAnimating];
[cell.contentView addSubview:imageView];

You can use animate method in custom cell class 您可以在自定义单元格类中使用动画方法

like this 像这样

@interface CustomCell ()

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        isAnimating = NO;
        [self startAni];
    }
    return self;
}

- (void)startAni{
    if (!isAnimating){
         isAnimating = YES;
    }
    //Animation Code here
}

- (void)stopAni{
    if (isAnimating){
         isAnimating = NO;
    }
    //Animation Code here
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM