collectionViewcell点击变色

网上找的方法: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]];
    
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,136评论 25 708
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,886评论 18 139
  • pod引用方式 pod 'XXLazyKit', :git => 'https://github.com/Pitt...
    PittWong阅读 303评论 0 0
  • 物质决定意识,意识对物质具有能动的反作用。曾经学习哲学,感觉没什么用处,好像空中楼阁,镜花水月,不觉得哲学可以引导...
    羊驼君子好逑阅读 571评论 0 0
  • 健身一是一般人能坚持的事,能坚持下来的人很不一般,健身本身就是一件非人类干的事,虐死人了~~ 不运动,怎么靠健康的...
    力量女侠阅读 413评论 0 0