Created
June 23, 2015 16:35
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
# 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; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Updated this to swift5
https://gist.github.com/SaurabhPrajapati/3a8fcf09c5a4db3d33c42ecb65856f67