Skip to content

Instantly share code, notes, and snippets.

@fahied
Created August 23, 2015 04:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fahied/08345196cbe9679506d4 to your computer and use it in GitHub Desktop.
Save fahied/08345196cbe9679506d4 to your computer and use it in GitHub Desktop.
Animate UITableViewCell along with Scrolling
/*
You have to hijack the scrollView (Add yourself as a ScrollViewDelegate alongside TableViewDelegate)
and the table view will automatically forward scrollview events along side tableview events.
(self.tableView.delegate = self) is really talking to both
<UIScrollViewDelegate, UITableViewDelegate>
I have a helper function in the example that also calculates distance to the top of the cell.
*/
-(void)scrollViewDidScroll:(UIScrollView *)scrollView {
NSArray *rows = [self.tableView indexPathsForVisibleRows];
for (NSIndexPath *path in rows) {
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:path];
float percent = [self cellDistanceAsPercentageFromTableViewCenterAtRow:cell];
cell.layer.sublayerTransform = CATransform3DMakeScale(percent, percent, 1);
}
}
//Calculate distance of the cell as a percentage from the bottom of the actual visible contentView
-(float)cellDistanceAsPercentageFromTableViewCenterAtRow:(UITableViewCell *)cell {
float position = cell.frame.origin.y;
float offsetFromTop = self.tableView.contentOffset.y;
float percentFromBottom = (position-offsetFromTop+ROW_HEIGHT)/self.tableView.frame.size.height;
percentFromBottom = MIN(MAX(percentFromBottom, 0), 1);
return percentFromBottom;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment