如何将手势从UITextView传递给UICollectionViewCell

我有一个横向滚动UICollectionView与UICollectionViewCells包含一个UITextView。 有没有办法将手势传递给单元格,以便didSelectItemAtIndexPath被调用? 我尝试了inheritanceUITextView并将touchesbegin / end传递给单元格,但是没有奏效。

您可以使视图非交互式,这将导致触摸通过:

textView.userInteractionEnabled = NO; 

如果你需要它是互动的,你可以试试这个:

 textView.editable = NO; UITapGestureRecognizer* tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapped)]; [textView addGestureRecognizer:tap]; 

…然后将此函数添加到您的UICollectionViewCell子类:

 -(void) tapped { UICollectionView *collectionView = (UICollectionView*)self.superview; NSIndexPath *indexPath = [collectionView indexPathForCell:self]; [collectionView.delegate collectionView:collectionView didSelectItemAtIndexPath:indexPath]; } 

我还没有testing它,但…

那么,如果你的单元格是文本视图的超级视图,那么你可以在UITextViewDelegate方法中实现类似这样的事情textViewDidBeginEditing: UITextViewDelegate textViewDidBeginEditing:

 - (void)textViewDidBeginEditing:(UITextView *)textView { NSIndexPath *indexPath = [self.collectionView indexPathForCell:(UICollectionViewCell *)textView.superview]; [self.collectionView selectItemAtIndexPath:indexPath animated:YES scrollPosition:UICollectionViewScrollPositionTop]; } 

这在iOS6.x中似乎不起作用:UICollectionViewCell中的全部视图似乎embedded在单元格的第一个子元素UIView中。 为了得到UITextView所在的实际单元格,您需要第二次解除引用。 换句话说,顺序是(从下到上):

UITextView-> enclosingUIView-> UICollectionViewCell