// 1.点击跳转时颜色的变化
// 2.实现UICollectionViewDelegate 协议里面的 高亮状态处理
#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
@interface UICollectionView (UpdaateCellBackground)
/// 更新cell被点击时的背景色
/// @param indexPath 点击的位置
- (void)updateCellClickedBackgroundColorAtIndexPath:(NSIndexPath *)indexPath;
@end
NS_ASSUME_NONNULL_END
#import "UICollectionView+UpdaateCellBackground.h"
@implementation UICollectionView (UpdaateCellBackground)
- (void)updateCellClickedBackgroundColorAtIndexPath:(NSIndexPath *)indexPath {
UICollectionViewCell *cell = [self cellForItemAtIndexPath:indexPath];
if (!cell) {
return;
}
cell.backgroundColor = kRGBColor(238, 238, 238);
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
cell.backgroundColor = kWhiteColor;
});
}
@end
#pragma mark -- UICollectionViewDelegate
- (void)collectionView:(UICollectionView *)collectionView didHighlightItemAtIndexPath:(NSIndexPath *)indexPath {
UICollectionViewCell *cell = [collectionView cellForItemAtIndexPath:indexPath];
cell.backgroundColor = kRGBColor(238, 238, 238);
}
- (void)collectionView:(UICollectionView *)collectionView didUnhighlightItemAtIndexPath:(NSIndexPath *)indexPath {
UICollectionViewCell *cell = [collectionView cellForItemAtIndexPath:indexPath];
cell.backgroundColor = kWhiteColor;
}
- (BOOL)collectionView:(UICollectionView *)collectionView shouldHighlightItemAtIndexPath:(NSIndexPath *)indexPath {
return true;
}