Craigt
Craigt

Reputation: 3580

UITableView Animation in cell causes smooth scrolling to break

I have a UITableView with custom UITableViewCells. Each cell asynchronously loads an image and displays it, this all works fine and the table view scrolls perfectly.

I have now added a "fade in" animation to the cell images, but this breaks the tables scrolling. When I flick, as soon as an image wants to "fade in" the scroll animation of the tableview will stop dead. Almost as if the newly introduced 'fade in' on the cell images interrupts the scroll animation.

Is there any way to bypass this? is this common behavior, or have I managed to do something really weird?

Here is the callback function that gets called once the image is done loading asynchronously (the animation function).

-(void)imageReady:(UIImage *)image FromUrl:(NSString *)url
{
    [UIView animateWithDuration:0.3 animations:^
    {
        [self.imageThumbnail setImage:image];
        self.imageThumbnail.alpha = 1;
    }]; 

}

I set the alpha to 0 at an earlier stage.

All help appreciated.

Upvotes: 1

Views: 2113

Answers (2)

Craigt
Craigt

Reputation: 3580

In my case, all it need was the "UIViewAnimationOptionAllowUserInteraction" option in the animateWithDuration call.

[UIView animateWithDuration:0.3 delay:0.0 options:(UIViewAnimationOptionCurveLinear | UIViewAnimationOptionAllowUserInteraction) animations:^
        {
            self.imageThumbnail.alpha = 1;
        } completion:nil];

Upvotes: 5

Mark Adams
Mark Adams

Reputation: 30846

Since UITableView descends from UIScrollView, I would implement the UIScrollViewDelegate methods, -scrollViewDidScroll: and -scrollViewDidEndDecelerating:. Additionally, I would add a BOOL to your class, something like shouldPerformAnimations.

Now in -scrollViewDidScroll, set shouldPerformAnimations to NO. Alternatively set it to YES in -scrollViewDidEndDecelerating:

Now all you have to do is check for shouldPerformAnimations before you perform the animation in imageReady:fromURL:. The idea is that if cells are scrolling by, it's pointless to perform animations that the user will never see. So by doing this we just wait until the table view has stopped scrolling, and then we perform the animations on all of the visible cells. You can obtain an array of index paths for the visible cells with the -indexPathForVisibleRows method on UITableView.

Upvotes: 1

Related Questions