views:

489

answers:

1

I'm trying to animate an image in a UITableViewCell subclass. It works when the tap on the cell is about 1/2 second or more in duration. For shorter taps, the cell gets selected, but my animation doesn't run.

In my view controller, I have:

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

    ImageCell *cell = (ImageCell*)[tableView cellForRowAtIndexPath:indexPath];
    cell.imageFlashDuration = 5.0;
    cell.imageFlashNumberOfFrames = 25;
    NSLog(@"Flash image...");
    [cell flashImage];
    [self performSelector:@selector(doSomething:) withObject:video afterDelay:5.0];
}

In my ImageCell:

-(void)flashImage {

    UIImage *image = imageView.image;
    if(imageView.isAnimating) {
        NSLog(@"Stop animating");
        [imageView stopAnimating];
    }
    NSMutableArray *animationArray = [NSMutableArray arrayWithCapacity:imageFlashNumberOfFrames];
    for(int i=0; i<imageFlashNumberOfFrames; i++) {
        [animationArray addObject:(i % 2 == 0? black60x60Image : image)];
    }
    imageView.animationImages = animationArray;
    imageView.animationDuration = imageFlashDuration;

    NSLog(@"> Start animating");
    [imageView startAnimating];
}

In my log, I see

2009-07-02 22:02:55.907 MyProg[1797:20b] Flash image...

2009-07-02 22:02:55.912 MyProg[1797:20b] > Start animating

2009-07-02 22:02:59.455 MyProg[1797:20b] Flash image...

2009-07-02 22:02:59.460 MyProg[1797:20b] > Start animating

2009-07-02 22:03:02.463 MyProg[1797:20b] Flash image...

2009-07-02 22:03:02.468 MyProg[1797:20b] > Start animating

2009-07-02 22:03:05.009 MyProg[1797:20b] Flash image...

2009-07-02 22:03:05.014 MyProg[1797:20b] > Start animating

The above was from a mixture of 'short' and 'long' touches. The long touches resulted in cell selection and image animation and the short ones resulted in cell selection without animation.

Additionally, if a short tap is followed by another short tap on the cell, the animation begins.

A: 

I think the problem lies in the didSelectRowAtIndexPath method. There you call the cellForRowAtIndexPath method, which will probably NOT give back the same cell that was clicked! The cell returned probably is a new cell, not visible on screen, with an animating image-thing on it. But... It's not visible.

I assume you handle the longer clicks in a different method (not didSelectRowAtIndexPath)?

You should probably keep a reference to the cell at the point the cell gets requested by the tableview itself (or assign a tag to it or something) in the cellForRowAtIndexPath method. Then when the didSelectRowAtIndexPath method, look up that specific instance of the cell (by the reference, or by the tag, ..)

Hope this helps :) Cheers

drvdijk
All touches are handled in the same method, but thanks for the lead. I'll give it a go.
edoloughlin
edoloughlin