Created
September 18, 2017 08:42
-
-
Save ratulSharker/463a4c3ef29251863cb90d2e86d08833 to your computer and use it in GitHub Desktop.
Highlighting a UICollectionViewCell while it's not been into the view window.
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
// | |
// UICollectionView+Highlight.h | |
// | |
// Created by Ratul Sharker on 9/18/17. | |
// | |
#import <UIKit/UIKit.h> | |
@interface UICollectionView (Highlight) | |
/* | |
* This method must be installed inside the | |
* cellForItemAtIndexPath. Before returning | |
* the cell to the UIKit, you must call this | |
* this method so all case of highlighting | |
* is covered. | |
*/ | |
-(void)pendingHighlightCell:(UICollectionViewCell*)cell | |
withIndexPath:(NSIndexPath*)indexPath; | |
/* | |
* call this method to highlight a particular cell | |
* with a particular background color. | |
*/ | |
-(void)highlightCollectionViewCellAtIndexPath:(NSIndexPath*)indexPath | |
withColor:(UIColor*)highColor; | |
@end |
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
// | |
// UICollectionView+Highlight.m | |
// | |
// Created by Ratul Sharker on 9/18/17. | |
// | |
#import "UICollectionView+Highlight.h" | |
@implementation UICollectionView (Highlight) | |
-(void)pendingHighlightCell:(UICollectionViewCell*)cell | |
withIndexPath:(NSIndexPath*)indexPath | |
{ | |
if(shouldHighlightInCellForItemAtIndexPath && | |
indexPath.row == highlightingIndexPath.row && | |
indexPath.section == highlightingIndexPath.section) | |
{ | |
// cell is already present in the scene | |
shouldHighlightInCellForItemAtIndexPath = NO; | |
UIColor *originalColor = cell.backgroundColor; | |
cell.backgroundColor = highlightingColor; | |
[UIView animateWithDuration:2.0 | |
animations:^{ | |
cell.backgroundColor = originalColor; | |
}]; | |
} | |
} | |
BOOL shouldHighlightInCellForItemAtIndexPath = NO; | |
static NSIndexPath *highlightingIndexPath; | |
static UIColor *highlightingColor; | |
-(void)highlightCollectionViewCellAtIndexPath:(NSIndexPath*)indexPath | |
withColor:(UIColor*)highColor | |
{ | |
UICollectionViewCell *cell = [self cellForItemAtIndexPath:indexPath]; | |
if(cell) | |
{ | |
// cell is already present in the scene | |
shouldHighlightInCellForItemAtIndexPath = NO; | |
UIColor *originalColor = cell.backgroundColor; | |
cell.backgroundColor = highColor; | |
[UIView animateWithDuration:2.0 | |
animations:^{ | |
cell.backgroundColor = originalColor; | |
}]; | |
} | |
else | |
{ | |
// cell is not present in the scene | |
// store the position | |
highlightingIndexPath = indexPath; | |
highlightingColor = highColor; | |
shouldHighlightInCellForItemAtIndexPath = YES; | |
} | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment