1.設(shè)置選中效果在UICollectionViewCell中設(shè)置背景顏色以及選中顏色。
? ? UIView* backgroundView = [[UIView alloc] initWithFrame:self.bounds];
? ? backgroundView.backgroundColor = [UIColor whiteColor];
? ? self.backgroundView= backgroundView;
? ? UIView* selectedBGView = [[UIView alloc] initWithFrame:self.bounds];
? ? selectedBGView.backgroundColor = [UIColor lightGrayColor];
? ? self.selectedBackgroundView= selectedBGView;
如果需要在選中中做點(diǎn)什么實(shí)現(xiàn)下面這個(gè)方法。
- (void)collectionView:(UICollectionView*)collectionView didSelectItemAtIndexPath:(NSIndexPath*)indexPath;
1.設(shè)置高亮效果在UICollectionViewCell中設(shè)置highlighted狀態(tài)就可以達(dá)到效果。
- (void)setHighlighted:(BOOL)highlighted
{
? ? if(highlighted) {
? ? ? ? self.backgroundView.backgroundColor = ?[UIColor lightGrayColor];
? ? }else
? ? {
? ? ? ? self.backgroundView.backgroundColor = nil;
?? ? }
}
UICollectionView中這三個(gè)方法只是告訴你現(xiàn)在是什么狀態(tài),不能在這里面設(shè)置選中顏色,不會(huì)有響應(yīng)的。
- (BOOL)collectionView:(UICollectionView*)collectionView shouldHighlightItemAtIndexPath:(NSIndexPath*)indexPath;
- (void)collectionView:(UICollectionView*)collectionView didHighlightItemAtIndexPath:(NSIndexPath*)indexPath;
- (void)collectionView:(UICollectionView*)collectionView didUnhighlightItemAtIndexPath:(NSIndexPath*)indexPath;
PS:選中顏色和高亮顏色最好不要同時(shí)設(shè)置,會(huì)出現(xiàn)錯(cuò)亂。