Forked from sebastienwindal/tableview & collectionview cell "pressed down" effect
Created
October 15, 2015 06:19
-
-
Save hardikamal/593688c73dfd7163ef3c to your computer and use it in GitHub Desktop.
Generic starting point to give a (more or less) subtle visual cue a tableview row is being selected.
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
/// table view | |
-(void) tableView:(UITableView *)tableView didHighlightRowAtIndexPath:(NSIndexPath *)indexPath | |
{ | |
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; | |
cell.contentView.alpha = 0.7; | |
[UIView animateWithDuration:0.15 | |
delay:0.0 | |
options:UIViewAnimationOptionCurveEaseInOut | |
animations:^{ | |
// "pressed down" effect. move the cell 2% to the left and reduce its size by 3%. | |
CGAffineTransform transform = CGAffineTransformMakeTranslation(-CGRectGetWidth(cell.contentView.frame) * 0.02, 0); | |
transform = CGAffineTransformScale(transform, 0.97, 0.97); | |
cell.contentView.transform = transform; | |
} | |
completion:nil]; | |
} | |
-(void) tableView:(UITableView *)tableView didUnhighlightRowAtIndexPath:(NSIndexPath *)indexPath | |
{ | |
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; | |
cell.contentView.alpha = 1.0; | |
[UIView animateWithDuration:0.1 | |
delay:0.0 | |
options:UIViewAnimationOptionCurveEaseInOut | |
animations:^{ | |
cell.contentView.transform = CGAffineTransformIdentity; | |
} | |
completion:nil]; | |
} | |
/// collection view | |
-(void) collectionView:(UICollectionView *)collectionView didHighlightItemAtIndexPath:(NSIndexPath *)indexPath | |
{ | |
UICollectionViewCell *cell = [collectionView cellForItemAtIndexPath:indexPath]; | |
cell.alpha = 0.7; | |
[UIView animateWithDuration:0.15 | |
delay:0.0 | |
options:UIViewAnimationOptionCurveEaseInOut | |
animations:^{ | |
// "pressed down" effect. move the cell 2% to the left and reduce its size by 3%. | |
CGAffineTransform transform = CGAffineTransformMakeScale(0.95, 0.95); | |
cell.transform = transform; | |
} | |
completion:nil]; | |
} | |
-(void) collectionView:(UICollectionView *)collectionView didUnhighlightItemAtIndexPath:(NSIndexPath *)indexPath | |
{ | |
UICollectionViewCell *cell = [collectionView cellForItemAtIndexPath:indexPath]; | |
cell.alpha = 1.0; | |
[UIView animateWithDuration:0.1 | |
delay:0.0 | |
options:UIViewAnimationOptionCurveEaseInOut | |
animations:^{ | |
cell.transform = CGAffineTransformIdentity; | |
} | |
completion:nil]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment