美文网首页
UICollectionViewCell 设置选择效果,点击效果

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

作者: Murphy___ | 来源:发表于2019-01-15 17:16 被阅读0次

    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:选中颜色和高亮颜色最好不要同时设置,会出现错乱。

    相关文章

      网友评论

          本文标题:UICollectionViewCell 设置选择效果,点击效果

          本文链接:https://www.haomeiwen.com/subject/kwmydqtx.html