简体   繁体   English

UITableViewCell中的UIImageView在动画UIImage被分配两次时不动画

[英]UIImageView in UITableViewCell not animating when an animated UIImage is assigned twice

I'm trying to display an animated UIImage in UITableViewCell's imageView. 我试图在UITableViewCell的imageView中显示一个动画UIImage。 The animated image is shown after the first assignment but is not on every further attempt. 动画图像在第一次分配后显示,但不是每次进一步尝试。

Here is the code of the TableViewController: 这是TableViewController的代码:

#import "ViewController.h"

@interface ViewController ()
@property (nonatomic, strong) UIImage *animatedImage;
@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.animatedImage = [UIImage animatedImageNamed:@"syncing" duration:1.0];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static BOOL second = NO;

    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    if (second) {

        cell.imageView.image = nil;
        NSLog(@"Removed image");
    }
    else {

        cell.imageView.image = self.animatedImage;
        NSLog(@"Added image");

        if (![cell.imageView isAnimating]) {

            [cell.imageView startAnimating];
            NSLog(@"Started animation for UIImageView: %@", cell.imageView);
        }
    }

    second = !second;

    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    [self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}

@end

And this is the output when I tap the cell two times: 当我点击两次单元格时,这是输出:

Added image
Started animation for UIImageView: <UIImageView: 0x7197540; frame = (0 0; 0 0); opaque = NO; userInteractionEnabled = NO; animations = { UIImageAnimation=<CAKeyframeAnimation: 0x715bc80>; }; layer = <CALayer: 0x71975a0>> - (null)
Removed image
Added image
Started animation for UIImageView: <UIImageView: 0x7197540; frame = (6 6; 30 30); opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x71975a0>> - (null)

I strongly suggest that you don't do any animation to the images in cellForRowAtIndexPath because if you have more images then an undesired effect will appear when you fast scroll (the cell images will switch due to cell re usability). 我强烈建议您不要对cellForRowAtIndexPath的图像进行任何动画处理,因为如果您有更多图像,则在快速滚动时会出现不需要的效果(单元格图像将因细胞可用性而切换)。

What I did in one of my projects, I implemented the – scrollViewDidScroll: method for the UITableViewDelegate (which conforms to UIScrollViewDelegate) and there I called a method that will animate the images only on the visible cells (tableView.visibleCells). 我在我的一个项目中做了什么,我为UITableViewDelegate实现了– scrollViewDidScroll:方法(符合UIScrollViewDelegate),我在那里调用了一个方法,它只在可见单元格上对图像进行动画处理(tableView.visibleCells)。

On the other hand I don't see why you are using the static BOOL second = NO; 另一方面,我不明白你为什么使用static BOOL second = NO; you could simply check if cell.imageView.image == nil or not. 你可以简单地检查cell.imageView.image ==是否为零。

Also check your animation code, it is possible that something is not working properly there (keep in mind that the cells are reused) . 还要检查你的动画代码,有可能某些东西在那里工作不正常(请记住,单元格被重用)。

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

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