Created
August 23, 2015 04:31
-
-
Save fahied/08345196cbe9679506d4 to your computer and use it in GitHub Desktop.
Animate UITableViewCell along with Scrolling
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
/* | |
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