Ios UILongPressGestureRecognitor在集合视图上打断单元格高亮显示

Ios UILongPressGestureRecognitor在集合视图上打断单元格高亮显示,ios,objective-c,uicollectionview,uigesturerecognizer,Ios,Objective C,Uicollectionview,Uigesturerecognizer,当我在我的collectionView上添加一个ui长按gestureRecognitor时(就像在中一样),collection view会停止按预期高亮显示单元格 我在UICollectionViewCellSubclass中处理单元格高亮显示,如中所述: 问题似乎是,当我在集合视图上添加ui长按gesturecognizer手势时,在将“突出显示”设置为“是”后,不会调用drawRect 如果我移动逻辑以在setHighlighted:YES中显示突出显示层,我将看不到触动的反馈,因为se

当我在我的collectionView上添加一个
ui长按gestureRecognitor
时(就像在中一样),collection view会停止按预期高亮显示单元格

我在UICollectionViewCellSubclass中处理单元格高亮显示,如中所述:

问题似乎是,当我在集合视图上添加
ui长按gesturecognizer
手势时,在将“突出显示”设置为“是”后,不会调用drawRect

如果我移动逻辑以在
setHighlighted:YES
中显示突出显示层,我将看不到触动的反馈,因为
setHighlighted:NO
之后会立即调用它!(只有在添加
UILongPressGestureRecognitor
后才会发生这种情况)


我还尝试移动
touchsbegind
/
touchsended
中的逻辑,但是
didSelectRowAtIndexPath:
不再工作。

不幸的是,
UILongPressGestureRecognitor
似乎阻止了UICollectionView高亮显示单元格所需的触摸。看起来没有一个好的方法来转发触摸,所以我想如果你想让它出现,你必须手动处理高亮显示。您可以通过setHighlighted以外的单独方法进行绘制,也可以尝试使用cell.highlighted属性进行绘制

下面是我为UICollectionView设计的一个示例

-(void)handleLongPress:(UILongPressGestureRecognizer *)gestureRecognizer
{
//    if (gestureRecognizer.state != UIGestureRecognizerStateEnded) {
//        return;
//    }
    CGPoint p = [gestureRecognizer locationInView:self.collectionView];

    NSIndexPath *indexPath = [self.collectionView indexPathForItemAtPoint:p];
    if (indexPath == nil){
        NSLog(@"couldn't find index path");
    } else {
        // get the cell at indexPath (the one you long pressed)
        UICollectionViewCell* cell = [self.collectionView cellForItemAtIndexPath:indexPath];
        if (gestureRecognizer.state != UIGestureRecognizerStateEnded) {
            if (!cell.highlighted) {
                [cell setHighlighted:YES];
            }
            return;
        }
        [cell setHighlighted:NO];
        // do stuff with the cell
    }
}

不幸的是,
UILongPressGestureRecognitor
似乎正在阻止UICollectionView高亮显示单元格所需的触摸。看起来没有一个好的方法来转发触摸,所以我想如果你想让它出现,你必须手动处理高亮显示。您可以通过setHighlighted以外的单独方法进行绘制,也可以尝试使用cell.highlighted属性进行绘制

下面是我为UICollectionView设计的一个示例

-(void)handleLongPress:(UILongPressGestureRecognizer *)gestureRecognizer
{
//    if (gestureRecognizer.state != UIGestureRecognizerStateEnded) {
//        return;
//    }
    CGPoint p = [gestureRecognizer locationInView:self.collectionView];

    NSIndexPath *indexPath = [self.collectionView indexPathForItemAtPoint:p];
    if (indexPath == nil){
        NSLog(@"couldn't find index path");
    } else {
        // get the cell at indexPath (the one you long pressed)
        UICollectionViewCell* cell = [self.collectionView cellForItemAtIndexPath:indexPath];
        if (gestureRecognizer.state != UIGestureRecognizerStateEnded) {
            if (!cell.highlighted) {
                [cell setHighlighted:YES];
            }
            return;
        }
        [cell setHighlighted:NO];
        // do stuff with the cell
    }
}

谢谢,我已经做过类似的事情了,但这只适用于长压,不适用于攻丝。如果我在我的collectionView上也放了一个UIAPTgestureRecognitor,那么DidSelectRowatineXpath就不起作用了…你可以从UIAPTgestureRecognitor选择器调用[self.collectionView DidSelectRowatineXpath:indexPath],以及做你想要的高亮显示谢谢,我已经做过类似的事情了,但这只适用于长压,不适用于攻丝。如果我在我的collectionView上也放置了一个UIAPTgestureRecognitor,那么DidSelectRowatineXpath就不再工作了……您可以从UIAPTgestureRecognitor选择器调用[self.collectionView DidSelectRowatineXpath:indexPath],也可以进行所需的高亮显示