3

我有一个使用自定义 UICollectionViewCell 的 UICollectionView,它又包含包含 UIImageView 的 UIScrollView。

UIScrollView 附有几个手势识别器(点击、双击)。我使用 UIScrollView 来放大图像。

问题是我能够在 UICollectionView 中接收触摸事件的唯一方法是禁用 UIScrollView 中的用户交互。尽管如此 UICollectionView 正在滚动,所以显然 UIScrollView 上的一些手势正在传递给父 UICollectionView)

我通过在 UIScrollView (自定义类)中添加这些方法尝试了另一种解决方案

@interface MyScrollView : UIScrollView

@end

@implentation MyScrollView
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    [[self nextResponder] touchesEnded:touches withEvent:event];
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [[self nextResponder] touchesBegan:touches withEvent:event];
}

-(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
    [[self nextResponder] touchesCancelled:touches withEvent:event];
}

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    [[self nextResponder] touchesMoved:touches withEvent:event];
}
@end

这有助于允许 collectionview 接收触摸,但是当在滚动视图中向上滑动时,collectionview 将停止接收未来的触摸......

传递触摸事件(单击/双击)的正确方法是什么?

4

2 回答 2

0

我有这个确切的设置,这就是我所做的

  1. 我在图像视图中添加了一个触摸手势

    UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(bannerTapped:)];
    singleTap.numberOfTapsRequired = 1;
    singleTap.numberOfTouchesRequired = 1;
    cell.conversationImageView.tag = indexPath.row;
    [cell.conversationImageView addGestureRecognizer:singleTap];
    [cell.conversationImageView setUserInteractionEnabled:YES];
    
  2. 然后我添加了这个

    - (void)bannerTapped:(UIGestureRecognizer *)gestureRecognizer {
        NSLog(@"%@", [gestureRecognizer view]);
        NSLog(@"the tag is %d", [gestureRecognizer view].tag);
    
        //do something here based on the tag which tells me what row I'm in
    }
    

希望也许这对你有用

于 2013-11-01T15:38:09.897 回答
0

你实现了gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer :?

见这里:UIGestureRecognizers 的多个手势(iPhone,Cocos2d)

于 2013-10-30T12:36:05.483 回答