繁体   English   中英

带图像的UICollectionView单元格,单击更改背景

[英]UICollectionView Cell with Image, change Background with click

我有一个带有Custom CollectionView CellsUICollectionView 在每个Cell上都有一个Image,它与整个Cell一样大。 现在我想在用户触摸Cell时突出显示Cell。 首先,我尝试使用以下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];
}

但没有任何事情发生。我删除了细胞中的图像,它完美地工作。 但随着图像没有任何反应! 我还可以做些什么?

如果您有CustomCell,则必须具有CustomCell.m(实现文件)。 在这个文件中添加这个,对我来说是简单的方法:

-(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
    }
}

在这里,您将图像放在整个单元格上,因此单元格位于图像后面,因图像而无法看到单元格背景。 最好拍摄两张图像一张用于高亮显示图像,另一张用于普通图像。 删除此行

cell.contentView.backgroundColor = [UIColor redColor];

设置突出显示的图像如下:

- (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];

}

设置正常图像如下:

- (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];
}

试试这样吧

- (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];
}

Swift版本的接受答案

class MyCollectionViewCell: UICollectionViewCell {

    override var highlighted: Bool {
        didSet {
            if (highlighted) {
                self.layer.opacity = 0.6;
            } else {
                self.layer.opacity = 1.0;
            }
        }
    }
}

资源

我发布这个答案是为了别人的利益,但我并不完全满意。 以下是我在初步观察中遇到的两个问题:

  1. 调光是突然的,不像UIButton水龙头那样动画。
  2. 第一列中的按钮根本不会改变外观。 也许这是我的应用程序中的一些错误,与此实际方法无关。 请在评论中确认或否认。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM