1

我的实现基于 UICollectionViewController。我尝试在 UiTableCell 上实现触摸行为以识别 touchdown 和 up 事件。我的解决方案基于一些研究,但我不确定这是完成它的正确方法。

我在每个表格单元格上附加了 UITapGestureRecognizer:

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {

    NSDictionary *homeItem = self.homeItems[indexPath.row];
    HomeCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:homeItem[@"cell"] forIndexPath:indexPath];

    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(cellClicked:)];
    //NSArray* recognizers = [cell gestureRecognizers];
    //    tapGesture.numberOfTapsRequired = 1;
    tapGesture.numberOfTouchesRequired = 1;

//    // Make the default gesture recognizer wait until the custom one fails.
//    for (UIGestureRecognizer* aRecognizer in recognizers) {
//        if ([aRecognizer isKindOfClass:[UITapGestureRecognizer class]])
//            [aRecognizer requireGestureRecognizerToFail:tapGesture];
//    }

    [cell addGestureRecognizer:tapGesture];

    return cell;
}

但是对于识别器状态 3 (UIGestureRecognizerStateEnded),只调用了一次声明的操作

- (void)cellClicked:(UIGestureRecognizer *)recognizer {
    Log(@"cellClicked with state: %d", [recognizer state]);
}

这一切都可能是由 CollectionView 行为的默认实现引起的。

我是 objc 和 ios 开发的新手,所以我不确定我能做些什么来完成我的实现,以通过状态更改单元格的样式。

4

1 回答 1

4

iOS 事件处理指南中所述,手势识别器有两种类型:离散连续。AUITapGestureRecognizer是离散的,这意味着它在识别其手势时只发送一个动作消息。

离散识别器,例如UITapGestureRecognizer在状态中发送动作UIGestureRecognizerStateRecognized,它是UIGestureRecognizerStateEnded.

如果您想分别识别触地和触地事件,您最好的选择可能只是使用自定义子类UICollectionViewCell(或您需要检测事件的任何视图),并覆盖touchesBegan:withEvent:相关消息。

于 2013-02-11T08:01:36.207 回答