如何将手势从UITextView传递到UICollectionViewCell

时间:2013-03-28 12:25:18

标签: ios uitextview uicollectionviewcell

我有一个水平滚动UICollectionView,其UICollectionViewCells包含一个UITextView。有没有办法将textview上的手势传递给单元格,以便调用didSelectItemAtIndexPath? 我尝试使用子类化UITextView并将touchesbegin / end传递给单元格,但这没有用。

3 个答案:

答案 0 :(得分:6)

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

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];
}

我还没有测试过它......

答案 1 :(得分:0)

好吧,如果您的单元格是文本视图的超级视图,则可以在UITextViewDelegate方法textViewDidBeginEditing:中实现类似的内容。

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

答案 2 :(得分:0)

这在iOS6.x中似乎不起作用:UICollectionViewCell中的所有视图似乎都嵌入在UIView中,该UIView是该单元的第一个子节点。为了获得UITextView所在的实际单元格,您需要再次取消引用。换句话说,顺序是(从下到上):

UITextView-> enclosingUIView-> UICollectionViewCell