我的实现基于 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 开发的新手,所以我不确定我能做些什么来完成我的实现,以通过状态更改单元格的样式。