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



(图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]];
}