更改UICollectionView单元格的alpha值无法正常工作

问题描述 投票:2回答:3

我想点击UICollectionView的单元格,单元格变为半透明。

    - (void)collectionView:(UICollectionView *)collectionView
didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    GLEpisodeCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:[GLEpisodeCell identifier] forIndexPath:indexPath];//GLEpisodeCell is subclass of UICollectionViewCell
    cell.alpha=0.5;
    cell.contentView.alpha=0.5;
}

但这不起作用。细胞仍然不透明。

ios cocoa-touch
3个回答
4
投票

尝试这样可能会对你有所帮助。

- (void)collectionView:(UICollectionView *)collectionView
didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    GLEpisodeCell *cell=[collectionView cellForItemAtIndexPath:indexPath];//GLEpisodeCell is subclass of UICollectionViewCell
    cell.alpha=0.5;
    cell.contentView.alpha=0.5;
}

2
投票

布局设置alpha。要覆盖,请将其放在您的单元格子类中:

override func apply(_ layoutAttributes: UICollectionViewLayoutAttributes) {
    alpha = 0.5
}

您还必须为新创建的单元格设置alpha。


0
投票

cell.backgroundColor = [UIColor colorWithRed:1 green:1 blue:1 alpha:0.5];

这将创建单元格的背景颜色为50%的透明度,而不是单元格的内容。如果你想要那么你想要的那样。 cell.backgroundView.alpha = 0.5;

© www.soinside.com 2019 - 2024. All rights reserved.