How to pass gestures from UITextView to UICollectionViewCell

坚强是说给别人听的谎言 提交于 2019-12-12 20:44:00

问题


I have a horizontal scrolling UICollectionView with UICollectionViewCells that contain a UITextView. Is there any way to pass gestures on the textview to the cells, so that didSelectItemAtIndexPath gets called?. I tried it with subclassing UITextView and passing touchesbegin/end to the cell, but that didn't worked.


回答1:


You can make the view non-interactive, which will cause touches to get passed through:

textView.userInteractionEnabled = NO;

If you need it to be interactive, you can try this:

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

... and then add this function to your UICollectionViewCell subclass:

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

I haven't tested it though...




回答2:


Well, if your cell is the superview of the text view, you could implement something like this in the UITextViewDelegate method textViewDidBeginEditing:.

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



回答3:


This doesn't seem to work in iOS6.x: the all view in a UICollectionViewCell seem to be embedded in a UIView that is the first child of the cell. In order to get the actual cell that is the UITextView is in you will need to dereference a second time. In other words the order is (from bottom to top):

UITextView->enclosingUIView->UICollectionViewCell



来源:https://stackoverflow.com/questions/15681765/how-to-pass-gestures-from-uitextview-to-uicollectionviewcell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!