網上找的方法: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]];
}