【问题标题】:Pass touch events from UIScrollview to UICollectionView将触摸事件从 UIScrollview 传递到 UICollectionView
【发布时间】:2013-11-09 23:47:23
【问题描述】:

我有一个使用自定义 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 将停止接收未来的触摸......

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

【问题讨论】:

    标签: ios uiscrollview uigesturerecognizer uicollectionview uicollectionviewcell


    【解决方案1】:

    【讨论】:

    • 您尝试过长按手势吗?我认为它会对你有所帮助;
    • 尝试 if (gesture state=statechanges) as toushes move
    • 我没有遇到滑动问题。只需轻按即可。点击在滚动视图中被调用,但不在集合视图中。在集合视图中调用滑动
    【解决方案2】:

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

    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
      }
      

    希望这对你有用

    【讨论】:

    • 所以在 uicollectionviewcell 中添加手势识别器?你如何将事件传递给collectionview
    • 现在调用了我的横幅,而不是使用 - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath 来作用于集合视图中的项目。这里的关键是标签,它告诉我我有哪些物品。 1)我的图像视图在集合视图中是可滚动的。 2)如果我点击它,我可以对它采取行动,因为我在显示它时添加了bannerTapped手势识别器。
    猜你喜欢
    • 1970-01-01
    • 2012-02-08
    • 1970-01-01
    • 2011-11-21
    • 1970-01-01
    • 2014-01-12
    • 2017-04-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多