我有UICollectionView
Custom CollectionView Cells
。在每个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];
}
但没有任何事情发生。我从细胞中删除了图像,它完美地工作。但随着图像没有任何反应!我还能做什么?
答案 0 :(得分:5)
如果您有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
}
}
答案 1 :(得分:4)
此处您将图像放在整个单元格上,因此单元格位于图像后面,因图像而无法看到单元格背景。最好拍摄两张图像一张用于高亮显示图像,另一张用于普通图像。 删除此行
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];
}
答案 2 :(得分:2)
试试这个
- (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];
}
答案 3 :(得分:0)
接受答案的Swift版本
class MyCollectionViewCell: UICollectionViewCell {
override var highlighted: Bool {
didSet {
if (highlighted) {
self.layer.opacity = 0.6;
} else {
self.layer.opacity = 1.0;
}
}
}
}
我发布这个答案是为了别人的利益,但我并不完全满意。以下是我在初步观察中遇到的两个问题:
UIButton
点击那样动画。