简体   繁体   English

在iOS中的UITableviewCell子视图中触摸Scrollview的事件

[英]Touch events of Scrollview in subview of UITableviewCell in iOS

I have tried searching over the internet about this problem but not able to come up with the solution that solves my problem. 我试过通过互联网搜索这个问题,但无法提出解决我的问题的解决方案。

I have make a subclass of UITableViewCell in which I added UIScrollView as a subview, now I want scroll gestures to be listened by scrolView and single tap gesture is listened by UItableView . 我做的一个子类UITableViewCell中,我加入UIScrollView作为一个子视图,现在我想滚动手势由scrolView被倾听并单击手势被倾听UItableView
Also using this layout is against the HIG of Apple 使用这种布局也是对抗Apple的HIG

This is an old question but since I didn't really find a good answer I figured I'd offer one. 这是一个老问题,但由于我没有找到一个好的答案,我想我会提供一个。 This approach captures the tap events on both the UIScrollView (contained 'within' the table cell) AND tap events on the UITableView cells directly, and passes them along to the UITableViewDelegate. 此方法捕获UIScrollView上的tap事件(包含在'表格单元格内)和直接在UITableView单元格上的tap事件,并将它们传递给UITableViewDelegate。 It also allows you to scroll your UIScrollView content without passing the scroll events along. 它还允许您滚动UIScrollView内容而不传递滚动事件。 Doesn't require any subclassing. 不需要任何子类。

  1. Within ViewDidLoad, instantiate a UITapGestureRecognizer on the UITableView 在ViewDidLoad中,在UITableView上实例化UITapGestureRecognizer
 // Capture taps on scrollView fields and pass along to tableView let tap = UITapGestureRecognizer(target: self, action: "tapIntercept:") tableView.addGestureRecognizer(tap) 
  1. Create a callback function for the selector above 为上面的选择器创建一个回调函数
 // Get the location of the table cell beneath the scrollView and pass the event off to the tableView func tapIntercept(recognizer: UIGestureRecognizer) { let point = recognizer.locationInView(tableView) if let indexPath = tableView.indexPathForRowAtPoint(point) { tableView(tableView, didSelectRowAtIndexPath: indexPath) } } 

If the scroll view is a subview of the table view, you can capture the gesture and send it to its parent view like this: 如果滚动视图是表视图的子视图,则可以捕获手势并将其发送到其父视图,如下所示:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM