【问题标题】:How to pass gestures from UITextView to UICollectionViewCell如何将手势从 UITextView 传递到 UICollectionViewCell
【发布时间】:2013-03-28 12:25:17
【问题描述】:

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

【问题讨论】:

    标签: ios uitextview uicollectionviewcell


    【解决方案1】:

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

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

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

    【讨论】:

    • 是的,但是我不能滚动。
    • 这里的问题是 selectItemAtIndexPath:animated:scrollPosition: 不会导致任何与选择相关的委托方法被调用
    • 但是 [cv.delegate collectionView:cv didSelectItemAtIndexPath:ip];没有
    • 如果您不需要交互,答案的第一部分在 iOS 8 上完美运行。
    【解决方案2】:

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

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

    【讨论】:

    • textViewDidBeginEditing 仅在文本被编辑时才被调用,但我不希望文本可编辑。
    • 这对我不起作用。 indexPathForCell 的结果为零。我可以得到单元格,而不是 indexPath。
    • @Micah 抱歉,我假设您使用的是 iOS 7?视图层次结构已更改,单元格现在存在于 view.superview.superview 中。如果您想要更动态的解决方案,请在此处查看我的帖子:stackoverflow.com/a/18255361/716216
    • 轰隆隆! @user716216 谢谢!那救了我。过去几个小时一直在做这个。
    【解决方案3】:

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

    UITextView->enclosureUIView->UICollectionViewCell

    【讨论】:

      猜你喜欢
      • 2014-07-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-22
      • 2013-09-21
      相关资源
      最近更新 更多