Skip to content

Instantly share code, notes, and snippets.

@floprr
Created June 23, 2015 16:35
Show Gist options
  • Save floprr/1b7a58e4a18449d962bd to your computer and use it in GitHub Desktop.
Save floprr/1b7a58e4a18449d962bd to your computer and use it in GitHub Desktop.
UITableView category method to smoothly animate in content (cells, headers and footers) after loading data.
/*
# Place this method inside a UITableView category.
# Usage:
[theTableView reloadData];
[theTableView layoutSubviews];
[theTableView tbl_animateInVisibleCells];
# If you don't want to use a category, replace 'self' with your tableView.
*/
- (void)tbl_animateInVisibleCells
{
NSMutableArray* visibleViews = self.visibleCells.mutableCopy;
NSArray* visibleSections = [self.indexPathsForVisibleRows valueForKeyPath:@"@distinctUnionOfObjects.section"];
for (NSNumber* section in visibleSections)
{
UIView* headerView = [self headerViewForSection:section.intValue];
if (headerView)
{
[visibleViews addObject:headerView];
}
UIView* footerView = [self footerViewForSection:section.intValue];
if (footerView)
{
[visibleViews addObject:footerView];
}
}
NSArray* sortedVisibleViews = [visibleViews sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
CGRect frame1 = [obj1 frame];
CGRect frame2 = [obj2 frame];
if (frame1.origin.y < frame2.origin.y)
{
return NSOrderedAscending;
}
else if (frame1.origin.y > frame2.origin.y)
{
return NSOrderedDescending;
}
else
{
return NSOrderedSame;
}
}];
for (UITableViewCell* cell in sortedVisibleViews)
{
cell.alpha = 0;
CGAffineTransform tx = CGAffineTransformIdentity;
tx = CGAffineTransformTranslate(tx, 0, 25);
tx = CGAffineTransformScale(tx, 0.95, 0.95);
cell.transform = tx;
cell.layer.shouldRasterize = YES;
cell.layer.rasterizationScale = [UIScreen mainScreen].scale;
}
CGFloat delay = 0;
for (UITableViewCell* cell in sortedVisibleViews)
{
[UIView animateWithDuration:0.75
delay:delay
usingSpringWithDamping:0.6
initialSpringVelocity:0
options:0
animations:^{
cell.alpha = 1;
cell.transform = CGAffineTransformIdentity;
} completion:^(BOOL finished) {
cell.layer.shouldRasterize = NO;
}];
delay += 0.025;
}
}
@SaurabhPrajapati
Copy link

SaurabhPrajapati commented Dec 31, 2019

@floprr
Copy link
Author

floprr commented Jun 8, 2020

Amazing! Thank you @SaurabhPrajapati :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment