ios 为什么 UICollectionView 的 UICollectionViewCell 没有在用户触摸时突出显示?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14637031/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why UICollectionView's UICollectionViewCell is not highlighting on user touch?
提问by IkegawaTaro
I have a UICollectionView that is made up of a custom UICollectionViewCell subclass. The cell's are displaying correctly and are responding correctly to user's touches by firing this method:
我有一个由自定义 UICollectionViewCell 子类组成的 UICollectionView。通过触发此方法,单元格正确显示并正确响应用户的触摸:
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
However, I am of the understanding that when a user touches the cell, it should highlight (in blue) and then the highlight should go away when the user lifts their finger. This is not happening. Any thoughts on why?
但是,我的理解是,当用户触摸单元格时,它应该突出显示(蓝色),然后当用户抬起手指时突出显示应该消失。这不会发生。关于为什么的任何想法?
Here is some relevant code:
这是一些相关的代码:
In the UICollectionView's datasource:
在 UICollectionView 的数据源中:
@implementation SplitCheckViewCollection
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
static NSString *cellIdentifier = @"ReceiptCellIdentifier";
SplitCheckCollectionCell *cell = (SplitCheckCollectionCell *)[collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];
cell.cellName.text = [NSString stringWithFormat:@"%@%i",@"#",indexPath.row+1];
return cell;
}
In the UICollectionViewCell's implementation:
在 UICollectionViewCell 的实现中:
@implementation SplitCheckCollectionCell
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
NSArray *arrayOfViews = [[NSBundle mainBundle] loadNibNamed:@"SplitCheckCollectionCell" owner:self options:nil];
if ([arrayOfViews count] < 1) {
return nil;
}
if (![[arrayOfViews objectAtIndex:0] isKindOfClass:[UICollectionViewCell class]]) {
return nil;
}
self = [arrayOfViews objectAtIndex:0];
}
return self;
}
采纳答案by SAE
The class only tells you about the highlight state, but doesn't change the visual appearance. You'll have to do it programmatically by e.g. changing the background of the cell.
该类只告诉您高亮状态,但不会改变视觉外观。您必须通过例如更改单元格的背景以编程方式执行此操作。
Details are described in the CollectionView Programming Guide.
详细信息在CollectionView 编程指南中描述。
回答by Ajaxharg
As SAE said,you have to do it yourself in a subclass. The other snag I just ran into is that when tapping a cell, it was receiving the highlight and redrawing if the cell was pressed and held. However, if tapped fast the redraw never happened.
正如 SAE 所说,你必须在子类中自己做。我刚刚遇到的另一个障碍是,当点击一个单元格时,如果按住该单元格,它会接收高亮和重绘。然而,如果快速点击重绘从未发生过。
I had created the cell in storyboard and the collection view has 'delays content touches' ticked as a default. I unticked this and it displayed instantly the finger touched the screen.
我在故事板中创建了单元格,并且集合视图默认勾选了“延迟内容触摸”。我取消勾选它,它立即显示手指触摸屏幕。
I am using a custom draw routine which checks the isHighlighted value. You also need to override setHighlighted in the custom cell as below or the draw routine never gets called.
我正在使用自定义绘制例程来检查 isHighlighted 值。您还需要覆盖自定义单元格中的 setHighlighted ,如下所示,否则永远不会调用绘制例程。
-(void)setHighlighted:(BOOL)highlighted
{
[super setHighlighted:highlighted];
[self setNeedsDisplay];
}
回答by ashakirov
There are 2 delegate methods you should to implement:
您应该实现 2 个委托方法:
- (void)collectionView:(UICollectionView *)colView didHighlightItemAtIndexPath:(NSIndexPath *)indexPath;
- (void)collectionView:(UICollectionView *)colView didUnhighlightItemAtIndexPath:(NSIndexPath *)indexPath;
Full code of highlighting collection view cell with animation:
使用动画突出显示集合视图单元格的完整代码:
- (void)collectionView:(UICollectionView *)colView didHighlightItemAtIndexPath:(NSIndexPath *)indexPath {
UICollectionViewCell* cell = [colView cellForItemAtIndexPath:indexPath];
//set color with animation
[UIView animateWithDuration:0.1
delay:0
options:(UIViewAnimationOptionAllowUserInteraction)
animations:^{
[cell setBackgroundColor:[UIColor colorWithRed:232/255.0f green:232/255.0f blue:232/255.0f alpha:1]];
}
completion:nil];
}
- (void)collectionView:(UICollectionView *)colView didUnhighlightItemAtIndexPath:(NSIndexPath *)indexPath {
UICollectionViewCell* cell = [colView cellForItemAtIndexPath:indexPath];
//set color with animation
[UIView animateWithDuration:0.1
delay:0
options:(UIViewAnimationOptionAllowUserInteraction)
animations:^{
[cell setBackgroundColor:[UIColor clearColor]];
}
completion:nil ];
}
回答by Basil Mariano
You need to implement the UICollectionViewDataSourceif you want to have Highlight and Unhighlight effect upon touch and release touch
如果您想在触摸和释放触摸时具有突出显示和取消突出显示效果,则需要实现UICollectionViewDataSource
here is the sample code
这是示例代码
#pragma mark - UICollectionView Datasource
- (void)collectionView:(UICollectionView *)colView didHighlightItemAtIndexPath:(NSIndexPath *)indexPath {
UICollectionViewCell* cell = [colView cellForItemAtIndexPath:indexPath];
cell.contentView.backgroundColor = [UIColor colorWithRed:235/255.0f green:236/255.0f blue:237/255.0f alpha:.5];
}
- (void)collectionView:(UICollectionView *)colView didUnhighlightItemAtIndexPath:(NSIndexPath *)indexPath {
UICollectionViewCell* cell = [colView cellForItemAtIndexPath:indexPath];
cell.contentView.backgroundColor = nil;
}
回答by troppoli
You can get a hilight to draw by adding these lines to your UICellView's setup.
您可以通过将这些线添加到 UICellView 的设置中来获得高亮显示。
UIView* selectedBGView = [[UIView alloc] initWithFrame:self.bounds];
selectedBGView.backgroundColor = [UIColor redColor];
self.selectedBackgroundView = selectedBGView;
From "Managing the Visual State for Selections and Highlights"... Collection views support single-item selection by default and can be configured to support multiple-item selection or have selections disabled altogether. The collection view detects taps inside its bounds and highlights or selects the corresponding cell accordingly. For the most part, the collection view modifies only the properties of a cell to indicate that it is selected or highlighted; it does not change the visual appearance of your cells, with one exception. If a cell's selectedBackgroundView property contains a valid view, the collection view shows that view when the cell is highlighted or selected.
从“管理选择和突出显示的视觉状态”...集合视图默认支持单项选择,并且可以配置为支持多项选择或完全禁用选择。集合视图检测其边界内的点击并相应地突出显示或选择相应的单元格。大多数情况下,集合视图仅修改单元格的属性以指示它被选中或突出显示;它不会改变细胞的视觉外观,只有一个例外。如果单元格的 selectedBackgroundView 属性包含有效视图,则当单元格突出显示或选中时,集合视图会显示该视图。
回答by Tim
As SAE points out you will have to manually do it in the cell on highlight. Easiest way I found is to use the tableview didHighlightRowAtIndexPath and didUnhighlightRowAtIndexPath methods which sets a bool "highlighted" in your UICollectionCell instance and then override that property in a subclassed UICollectionCell class. The beauty of this is that the animation is already there for you. You can also do the same in a UITableView/UITableViewCell situation.
正如 SAE 指出的那样,您必须在突出显示的单元格中手动执行此操作。我发现的最简单的方法是使用 tableview didHighlightRowAtIndexPath 和 didUnhighlightRowAtIndexPath 方法,它们在您的 UICollectionCell 实例中设置一个布尔“突出显示”,然后在子类 UICollectionCell 类中覆盖该属性。这样做的美妙之处在于动画已经为您准备好了。你也可以在 UITableView/UITableViewCell 的情况下做同样的事情。
So in your UICollectionView using the UICollectionViewDelegate method:
所以在你的 UICollectionView 中使用 UICollectionViewDelegate 方法:
func collectionView(collectionView: UICollectionView, didHighlightItemAtIndexPath indexPath: NSIndexPath) {
collectionView.selectItemAtIndexPath(indexPath, animated: true, scrollPosition: UICollectionViewScrollPosition.None)
}
func collectionView(collectionView: UICollectionView, didUnhighlightItemAtIndexPath indexPath: NSIndexPath) {
collectionView.deselectItemAtIndexPath(indexPath, animated: true)
}
Then in your UICollectionViewCell subclass add this:
然后在您的 UICollectionViewCell 子类中添加以下内容:
override var highlighted:Bool{
didSet{
println("Highlighted is set \(highlighted)")
if(highlighted == true){
self.backgroundColor = UIColor.redColor()
}else{
self.backgroundColor = UIColor.blueColor()
}
}
}
回答by KSR
We can create our own highlight and unhighlight effect on collectionView cell by adding and removing a temporary view with some background color as follows:
-(void)collectionView:(UICollectionView *)collectionView didHighlightItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell *collectionViewCell = (UICollectionViewCell *)
[collectionView cellForItemAtIndexPath:indexPath];
UIView *tapHighlightView = (UIView*)[collectionViewCell.contentView
viewWithTag:10];
if (!tapHighlightView) {
tapHighlightView = [[UIView alloc]
initWithFrame:collectionViewCell.contentView.frame];
tapHighlightView.backgroundColor =[UIColor blackColor alpha:0.4];
tapHighlightView.tag = 10;
[collectionViewCell.contentView addSubview:tapHighlightView];
}
}
-(void)collectionView:(UICollectionView *)collectionView didUnhighlightItemAtIndexPath:(NSIndexPath *)indexPath{
UICollectionViewCell *collectionViewCell = (UICollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath];
UIView *tapHighlightView = (UIView*)[collectionViewCell.contentView viewWithTag:10];
if (tapHighlightView != nil) {
[tapHighlightView removeFromSuperview];
}
}
回答by Ha cong Thuan
you can try this code :
你可以试试这个代码:
- (void)collectionView:(UICollectionView *)colView didHighlightItemAtIndexPath:(NSIndexPath *)indexPath {
UICollectionViewCell* cell = [colView cellForItemAtIndexPath:indexPath];
cell.contentView.backgroundColor = [UIColor blueColor];
}
and
和
- (void)collectionView:(UICollectionView *)colView didUnhighlightItemAtIndexPath:(NSIndexPath *)indexPath {
UICollectionViewCell* cell = [colView cellForItemAtIndexPath:indexPath];
cell.contentView.backgroundColor = nil;
}
回答by Frank
If you want to change the visuals you can set the cell to selected on the didHighlightItemAtIndexPath and deselect on didHighlightItemAtIndexPath like the following:
如果要更改视觉效果,可以将单元格设置为在 didHighlightItemAtIndexPath 上选中,然后在 didHighlightItemAtIndexPath 上取消选中,如下所示:
- (BOOL)collectionView:(UICollectionView *)collectionView shouldHighlightItemAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
- (void)collectionView:(UICollectionView *)collectionView didHighlightItemAtIndexPath:(NSIndexPath *)indexPath
{
[collectionView selectItemAtIndexPath:indexPath animated:YES scrollPosition:NO];
}
- (void)collectionView:(UICollectionView *)collectionView didUnhighlightItemAtIndexPath:(NSIndexPath *)indexPath
{
[collectionView deselectItemAtIndexPath:indexPath animated:YES];
}