点击时更改 UICollectionView 单元格的背景 [英] Change Background of UICollectionView Cell on Tap

查看:14
本文介绍了点击时更改 UICollectionView 单元格的背景的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个以编程方式创建的 UICollectionView.我希望集合视图的行为方式如下:

I have a UICollectionView that I have created programmatically. I would like for the collection view to behave in the following way:

1. User touches cell
2. Cell background color changes
3. User releases touch
4. Cell background color changes

这应该是在执行与点击操作相关的选择器之前发生的快速颜色更改,其中包含集合视图的视图控制器从堆栈中弹出.

This should be a quick color change that happens just before the selector related to the tap action is executed in which the viewcontroller containing the collection view is popped off the stack.

我一直在研究这个问题:UICollectionView 单元格在点击时更改背景

I have been looking at this question: UICollectionView cell change background while tap

其中有以下用于此目的的方法摘要:

in which there is the following summary of methods to use for this purpose:

// Methods for notification of selection/deselection and highlight/unhighlight events.
// The sequence of calls leading to selection from a user touch is:
//
// (when the touch begins)
// 1. -collectionView:shouldHighlightItemAtIndexPath:
// 2. -collectionView:didHighlightItemAtIndexPath:
//
// (when the touch lifts)
// 3. -collectionView:shouldSelectItemAtIndexPath: or -    collectionView:shouldDeselectItemAtIndexPath:
// 4. -collectionView:didSelectItemAtIndexPath: or -collectionView:didDeselectItemAtIndexPath:
// 5. -collectionView:didUnhighlightItemAtIndexPath:

我假设我只需要从触摸开始时"和触摸结束时"实现上述方法之一.但无论我做什么,似乎背景颜色都会发生变化,然后保持变化.这是我尝试过的一个例子,但没有成功:

I am assuming I only need to implement one of the above methods from 'when touch begins' and 'when touch ends.' But no matter what I do, it appears that a background color changes and then remains changed. Here is an example of something I attempted which did not work:

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
 {
   //pop vc 
 }

- (void)collectionView:(UICollectionView *)collectionView didHighlightItemAtIndexPath:(NSIndexPath *)indexPath
{
  UICollectionViewCell* cell = [collectionView cellForItemAtIndexPath:indexPath];
  cell.contentView.backgroundColor = [UIColor redColor];
}

- (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath
{
  UICollectionViewCell *cell = [collectionView cellForItemAtIndexPath:indexPath];
  cell.contentView.backgroundColor = [UIColor greenColor];
}

这会导致单元格背景颜色仅更改为红色.我还查看了这个问题:UICollectionView 选择和取消选择问题 并尝试实现 [UICollectionView selectItemAtIndexPath:animated:scrollPosition:] 并在 didSelectItemAtIndexPath 内部调用它,但这也不起作用.集合视图数据源和委托已设置.

This results in the cell background color being changed only to red. I also looked at this question: UICollectionView Select and Deselect issue and tried implementing [UICollectionView selectItemAtIndexPath:animated:scrollPosition:] and calling it inside of didSelectItemAtIndexPath, but this did not work either. Collection view data source and delegate are set.

推荐答案

问题是您正在更改 highlight 上的颜色并在 deselect 上将其更改回取消突出显示

The problem is that you are changing the color on highlight and changing it back on deselect instead that on unhighlight

你应该简单地改变这个:

You should simply change this:

- (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath
{
  UICollectionViewCell *cell = [collectionView cellForItemAtIndexPath:indexPath];
  cell.contentView.backgroundColor = [UIColor greenColor];
}

到这里:

- (void)collectionView:(UICollectionView *)collectionView didUnhighlightItemAtIndexPath:(NSIndexPath *)indexPath
{
  UICollectionViewCell *cell = [collectionView cellForItemAtIndexPath:indexPath];
  cell.contentView.backgroundColor = [UIColor greenColor];
}

另外,如果您不想在高亮显示之前稍等片刻,您应该将集合视图的 delaysContentTouches 属性设置为 NO

Also, if you don't want to wait a bit before getting your highlight happen you should set the delaysContentTouches property of the collection view to NO

还要确保你调用

[collectionView deselectItemAtIndexPath:indexPath animated:NO];

在 -didSelectItemAtIndexPath 方法中

inside the -didSelectItemAtIndexPath method

这篇关于点击时更改 UICollectionView 单元格的背景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆