UICollectionViewCell 设置选择效果,点击效果

2019-01-15  本文已影响0人  Murphy___

1.设置选中效果在UICollectionViewCell中设置背景颜色以及选中颜色。

    UIView* backgroundView = [[UIView alloc] initWithFrame:self.bounds];

    backgroundView.backgroundColor = [UIColor whiteColor];

    self.backgroundView= backgroundView;

    UIView* selectedBGView = [[UIView alloc] initWithFrame:self.bounds];

    selectedBGView.backgroundColor = [UIColor lightGrayColor];

    self.selectedBackgroundView= selectedBGView;

如果需要在选中中做点什么实现下面这个方法。

- (void)collectionView:(UICollectionView*)collectionView didSelectItemAtIndexPath:(NSIndexPath*)indexPath;

1.设置高亮效果在UICollectionViewCell中设置highlighted状态就可以达到效果。

- (void)setHighlighted:(BOOL)highlighted

{

    if(highlighted) {

        self.backgroundView.backgroundColor =  [UIColor lightGrayColor];

    }else

    {

        self.backgroundView.backgroundColor = nil;

     }

}

UICollectionView中这三个方法只是告诉你现在是什么状态,不能在这里面设置选中颜色,不会有响应的。

- (BOOL)collectionView:(UICollectionView*)collectionView shouldHighlightItemAtIndexPath:(NSIndexPath*)indexPath;

- (void)collectionView:(UICollectionView*)collectionView didHighlightItemAtIndexPath:(NSIndexPath*)indexPath;

- (void)collectionView:(UICollectionView*)collectionView didUnhighlightItemAtIndexPath:(NSIndexPath*)indexPath;

PS:选中颜色和高亮颜色最好不要同时设置,会出现错乱。

上一篇下一篇

猜你喜欢

热点阅读