Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I'm trying to display an animated UIImage in UITableViewCell's imageView. The animated image is shown after the first assignment but is not on every further attempt.

Here is the code of the 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)
like image 978
Pipo Avatar asked Nov 13 '22 05:11

Pipo


1 Answers

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).

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).

On the other hand I don't see why you are using the static BOOL second = NO; you could simply check if cell.imageView.image == nil or not.

Also check your animation code, it is possible that something is not working properly there (keep in mind that the cells are reused) .

like image 155
danypata Avatar answered Nov 15 '22 07:11

danypata