ScrollView手势识别器吃掉所有触摸事件

时间:2013-06-02 12:23:42

标签: ios objective-c uiscrollview uitapgesturerecognizer

我有一个UIScrollView我添加了一个单击手势识别器来显示/隐藏一些UI叠加层:

UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)];
[scrollView addGestureRecognizer:singleTap];

- (void)handleTap:(UITapGestureRecognizer *)sender {
    // report click to UI changer
}

我在UIScrollView的底部添加了easy table view。一切正常(水平和垂直滚动)但问题是点击仅由手势识别器(上图)识别,而不是由简单的表格视图识别。 如果我删除注册手势监听器的行,一切正常,表视图会注意到它自己。

就好像手势识别器功能“吃掉”表视图上的点击事件并且不会向下传播它们。

感谢任何帮助

9 个答案:

答案 0 :(得分:75)

这可以解决您的问题 Detect touch event on UIScrollView AND on UIView's components [which is placed inside UIScrollView]
想法是告诉手势识别器不要吞下触摸事件。为此,您需要将singleTap的cancelsTouchesInView属性设置为NO,默认为YES

UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)];
singleTap.cancelsTouchesInView = NO;
[scrollView addGestureRecognizer:singleTap]; 

答案 1 :(得分:10)

Swift 3.0

 let singleTap = UITapGestureRecognizer(target: self, action: #selector(handleTap(_:)))
 singleTap.cancelsTouchesInView = false
 singleTap.numberOfTapsRequired = 1
 scrollView.addGestureRecognizer(singleTap)

选择器方法就像。

@objc func handleTap(_ recognizer: UITapGestureRecognizer) {
  // Perform operation
}

答案 2 :(得分:6)

您可以设置要包含/排除哪些对象进行触摸。

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gesture shouldReceiveTouch:(UITouch *)touch {            
   if (touch.view == [self view]) {
       return YES;
   }
   return NO;
}

答案 3 :(得分:6)

我认为原因是U User Interaction Enabled为UIImageView设置为false。您应该将其设置为true以启用其中的点击

答案 4 :(得分:2)

谢谢@zambrey

Swift 2.2+版本:

scrollView.delegate = self

let allowMultipleTouches = UITapGestureRecognizer(target: self, action: #selector(genderPressed))
allowMultipleTouches.numberOfTapsRequired = 1
allowMultipleTouches.cancelsTouchesInView = false

scrollView.addGestureRecognizer(allowMultipleTouches)

如果您的滚动视图位于故事板中,请不要忘记在视图控制器中固定插座。在此示例中,scrollViewUIScrollView的出口。

答案 5 :(得分:2)

这对我有用 Swift 3 / Xcode 8

    self.scrollView.touchesShouldCancel(in: ** the view you want the touches in **)
    self.scrollView.canCancelContentTouches = false
祝你好运!

答案 6 :(得分:1)

TapGestures为我工作。另一方面,滑动,我必须禁用滚动,它工作。

     swipeLeftGesture = UISwipeGestureRecognizer(target: self, action: #selector(swipeToNewImage(_:)))
     swipeLeftGesture.direction = .left
     scrollView.addGestureRecognizer(swipeLeftGesture)

     swipeRightGesture = UISwipeGestureRecognizer(target: self, action: #selector(swipeToNewImage(_:)))
     scrollView.addGestureRecognizer(swipeRightGesture)

    scrollView.isScrollEnabled = false

答案 7 :(得分:1)

您可以在UIscrollView中捕获任何类型的手势。确保您还处理一些默认属性,例如将cancelsTouchesInView属性设置为false,默认情况下为true。 还要为子视图指定一些标记,以区分选择器。 &安培;也使他们的用户互动成为现实。

  

让tap = UITapGestureRecognizer(目标:自我,行动​​:

     

选择器(didTapByUser(_:)))

答案 8 :(得分:0)

我的代码。

我检查了所有建议的解决方案以及对我而言的所有工作。我不懂为什么。我不明白原因。

class MyClass: UITableViewController {

override func viewDidLoad() {
    super.viewDidLoad()

let scrollView = UIScrollView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.size.width, height: UIScreen.main.bounds.size.height))

scrollView.contentSize = CGSize(width: UIScreen.main.bounds.size.width, height: 800)

scrollView.isUserInteractionEnabled = true
scrollView.delegate = self

let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(manageGesture))

tap.cancelsTouchesInView = false

tap.numberOfTapsRequired = 1

scrollView.addGestureRecognizer(tap)

scrollView.canCancelContentTouches = false

self.view.addSubview(scrollView)

}

@objc func manageGesture(){
    // Some action
}


}