美文网首页
collectionViewcell点击变色

collectionViewcell点击变色

作者: 拖不垮打不烂 | 来源:发表于2017-08-12 11:44 被阅读0次

网上找的方法:collectionViewcell点击变色,我发现还是需要长按才能实现效果(不排除我写的代码有问题),所以自己做了一个,思路是每次点击,创建个定时器,由定时器管理颜色变化.

默认为白色(图1)
点击变为亮灰色(图2)
恢复背景颜色(图3)
(图1)代码如下:
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    NSLog(@"众筹");
    // collectionViewcell嵌套在HomePageSixTableViewCell里
    HomePageSixTableViewCell *cell = (HomePageSixTableViewCell*)[collectionView cellForItemAtIndexPath:indexPath];
    __weak HomePageSixTableViewCell * weakCell = cell;
    [cell setBackgroundColor:[UIColor lightGrayColor]];
    _touchTimer = [NSTimer scheduledTimerWithTimeInterval:0.1 repeats:NO block:^(NSTimer * _Nonnull timer) {
        //想干啥坏事写在这里面
        [weakCell setBackgroundColor:[UIColor whiteColor]];
    }];
}

(图2)代码如下:

-(void) collectionView:(UICollectionView *)collectionView didHighlightItemAtIndexPath:(NSIndexPath *)indexPath{
    //设置选中时的颜色
    HomePageSixTableViewCell *cell = (HomePageSixTableViewCell*)[collectionView cellForItemAtIndexPath:indexPath];
    [cell setBackgroundColor:[UIColor lightGrayColor]];
}

(图3)代码如下:

- (void)collectionView:(UICollectionView *)colView  didUnhighlightItemAtIndexPath:(NSIndexPath *)indexPath{
    //恢复颜色
    HomePageSixTableViewCell *cell = (HomePageSixTableViewCell*)[colView cellForItemAtIndexPath:indexPath];
    [cell setBackgroundColor:[UIColor whiteColor]];
    
}

相关文章

网友评论

      本文标题:collectionViewcell点击变色

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