Why UICollectionView's UICollectionViewCell is not highlighting on user touch?
The class only tells you about the highlight state, but doesn't change the visual appearance. You'll have to do it programmatically by e.g. changing the background of the cell.
Details are described in the CollectionView Programming Guide.
As SAE said,you have to do it yourself in a subclass. The other snag I just ran into is that when tapping a cell, it was receiving the highlight and redrawing if the cell was pressed and held. However, if tapped fast the redraw never happened.
I had created the cell in storyboard and the collection view has 'delays content touches' ticked as a default. I unticked this and it displayed instantly the finger touched the screen.
I am using a custom draw routine which checks the isHighlighted value. You also need to override setHighlighted in the custom cell as below or the draw routine never gets called.
-(void)setHighlighted:(BOOL)highlighted
{
[super setHighlighted:highlighted];
[self setNeedsDisplay];
}
There are 2 delegate methods you should to implement:
- (void)collectionView:(UICollectionView *)colView didHighlightItemAtIndexPath:(NSIndexPath *)indexPath;
- (void)collectionView:(UICollectionView *)colView didUnhighlightItemAtIndexPath:(NSIndexPath *)indexPath;
Full code of highlighting collection view cell with animation:
- (void)collectionView:(UICollectionView *)colView didHighlightItemAtIndexPath:(NSIndexPath *)indexPath {
UICollectionViewCell* cell = [colView cellForItemAtIndexPath:indexPath];
//set color with animation
[UIView animateWithDuration:0.1
delay:0
options:(UIViewAnimationOptionAllowUserInteraction)
animations:^{
[cell setBackgroundColor:[UIColor colorWithRed:232/255.0f green:232/255.0f blue:232/255.0f alpha:1]];
}
completion:nil];
}
- (void)collectionView:(UICollectionView *)colView didUnhighlightItemAtIndexPath:(NSIndexPath *)indexPath {
UICollectionViewCell* cell = [colView cellForItemAtIndexPath:indexPath];
//set color with animation
[UIView animateWithDuration:0.1
delay:0
options:(UIViewAnimationOptionAllowUserInteraction)
animations:^{
[cell setBackgroundColor:[UIColor clearColor]];
}
completion:nil ];
}