Ios UICollectionviewCell图像在选择时更改
我有Ios UICollectionviewCell图像在选择时更改,ios,objective-c,uicollectionview,uicollectionviewcell,Ios,Objective C,Uicollectionview,Uicollectionviewcell,我有UICollectionview控制器,每个单元格中都有UIButton。选择按钮时,我必须更改按钮bg图像,双击时,我必须再次更改它。(在“用户兴趣”页面上,单击一次表示喜欢,双击表示喜欢的概念)。 最好的方法是什么 - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{ UICollec
UICollectionview
控制器,每个单元格中都有UIButton
。选择按钮时,我必须更改按钮bg图像,双击时,我必须再次更改它。(在“用户兴趣”页面上,单击一次表示喜欢,双击表示喜欢的概念)。
最好的方法是什么
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
UICollectionViewCell *cell = [interestsCollection dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];
UIButton* img=[[UIButton alloc]init];
[img setImage:[UIImage imageNamed:[imgTitleArray objectAtIndex:indexPath.row]] forState:UIControlStateNormal];
[img addTarget: self action: @selector(interestClicked:) forControlEvents: UIControlEventTouchUpInside] ;
img.frame=CGRectMake(10, 10, 60, 60);
[cell.contentView addSubview:img];
UILabel* lbl=[[UILabel alloc]initWithFrame:CGRectMake(0, 75, 80, 20)];
lbl.textAlignment = NSTextAlignmentCenter;
lbl.text=[titleArray objectAtIndex:indexPath.row];
lbl.font = [UIFont fontWithName:@"Arial" size:11];
lbl.textColor = [UIColor colorWithRed:135.0/255.0 green:135.0/255.0 blue:135.0/255.0 alpha:1.0];
[cell.contentView addSubview:lbl];
return cell;
}
您可以使用单键和双键
tapperstate
处理这种情况。在受尊敬的选择器的帮助下,您可以更改ui按钮的ui图像
以下是指导您的链接。如何在UICollectionView
上创建双击手势。借助于此,您还可以创建单点击手势
将UITapGestureRecognitizer
添加到按钮中,并根据需要更改颜色
UITapGestureRecognizer *tapOnce = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapOnce:)];
UITapGestureRecognizer *tapTwice = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapTwice:)];
tapOnce.numberOfTapsRequired = 1;
tapTwice.numberOfTapsRequired = 2;
//stops tapOnce from overriding tapTwice
[tapOnce requireGestureRecognizerToFail:tapTwice];
[self.button addGestureRecognizer:tapOnce]; //remove the other button action which calls method `button`
[self.button addGestureRecognizer:tapTwice];
你实现了这个方法吗-(void)collectionView:(UICollectionView*)collectionView didSelectItemAtIndexPath:(NSIndexPath*)indexPath{}是的,我有这个方法,但如何在选择时更改所选单元格的子视图(按钮)背景?-(void)有趣的点击:(id)发送方{UIButton*按钮=(UIButton*)发送方;//在此处更改按钮图像[collectionView重新加载数据];}