UICollectionView Cell with image, change background with click
I have UICollectionView
c Custom CollectionView Cells
. Each cell has an image on it, as large as the entire Cell. Now I want to highlight the Cell when the User touches the cell. I first tried it with the following delegate Methods
:
- (void)collectionView:(UICollectionView *)collectionView didHighlightItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell* cell = [self.collectionView cellForItemAtIndexPath:indexPath];
cell.contentView.backgroundColor = [UIColor redColor];
}
- (void)collectionView:(UICollectionView *)collectionView didUnhighlightItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell *cell = [self.collectionView cellForItemAtIndexPath:indexPath];
cell.contentView.backgroundColor = [UIColor clearColor];
}
But nothing happened. After I removed "Images from Cells" it worked great. But nothing happens to the images! What else can I do?
source to share
If you have CustomCell you should have CustomCell.m (implementation file). In this file add this, for me it's an easy way:
-(void)setHighlighted:(BOOL)highlighted
{
if (highlighted)
{
self.layer.opacity = 0.6;
// Here what do you want.
}
else{
self.layer.opacity = 1.0;
// Here all change need go back
}
}
source to share
Here you put the whole image over the cell, so the cell is behind the image, you cannot see the background of the cell because of the image. Better to take two images, one for the selected image and the other for the normal one. remove this line
cell.contentView.backgroundColor = [UIColor redColor];
set the highlighted image like this:
- (void)collectionView:(UICollectionView *)collectionView didHighlightItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell* cell = [self.collectionView cellForItemAtIndexPath:indexPath];
UIImage *image11 = [UIImage imageNamed:@"your_highlighted_image"];
CGRect starFrame1 = CGRectMake(0, 0, 350, 50);
UIImageView *starImage2 = [[UIImageView alloc] initWithFrame:starFrame1];
starImage2.image= image11;
//set this image as cell background
[cell setBackgroundView:starImage2];
}
set normal image like this:
- (void)collectionView:(UICollectionView *)collectionView didUnhighlightItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell *cell = [self.collectionView cellForItemAtIndexPath:indexPath];
UIImage *image11 = [UIImage imageNamed:@"your_normal_image"];
CGRect starFrame1 = CGRectMake(0, 0, 350, 50);
UIImageView *starImage2 = [[UIImageView alloc] initWithFrame:starFrame1];
starImage2.image= image11;
//set this image as cell background
[cell setBackgroundView:starImage2];
}
source to share
try it
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
UICollectionViewCell *selectedCell =
[collectionView cellForItemAtIndexPath:indexPath];
selectedCell.contentView.backgroundColor = nil;
[selectedCell.contentView.layer setBorderColor:[UIColor redColor].CGColor];
[selectedCell.contentView.layer setBorderWidth:3.0f];
}
- (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell *deselectedCell =
[collectionView cellForItemAtIndexPath:indexPath];
deselectedCell.contentView.backgroundColor = nil;
[deselectedCell.contentView.layer setBorderColor:[UIColor clearColor].CGColor];
[deselectedCell.contentView.layer setBorderWidth:3.0f];
}
source to share
Swift version of the accepted answer
class MyCollectionViewCell: UICollectionViewCell {
override var highlighted: Bool {
didSet {
if (highlighted) {
self.layer.opacity = 0.6;
} else {
self.layer.opacity = 1.0;
}
}
}
}
I am posting this answer for the benefit of others, but I am not completely satisfied with it. Here are two problems I ran into in my initial observation:
- The dimming is sharp and not animated like a tap
UIButton
. - The buttons in the first column do not change their appearance at all. It might be some bug in my application and not related to this actual method. Please confirm or reject this in the comments.
source to share