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

查看:104
本文介绍了更改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.

推荐答案

问题是您正在更改突出显示的颜色并在取消选择上更改它,而不是取消选择

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

您只需更改此内容:

- (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 属性设置为

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];

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

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