19

I have a Scrollview with a button on it. I have an action set to TouchupInside. Works fine initially. So I need the Scrollview will autohide if it hasnt been touched in 3 seconds. To accomplish this i am using UITapGestureRecognizer on the Scrollview. It definitely works if you touch anywhere in the Scrollview. Unfortunately the UIButton no longer works. The UITapGestureRecognizer seems to supersede the button.

Any ideas how to get the UIButton Action to work?

Abbacore
  • 385
  • 1
  • 3
  • 8

2 Answers2

49

You can stop the UITapGestureRecognizer from cancelling other events using this line:

tapRecognizer.cancelsTouchesInView = NO;

Worked for me :)

jowie
  • 8,028
  • 8
  • 55
  • 94
0

Another option is to prevent firing Gesture when the tap is on some sort of embedded control. For example:

class ViewController: UIViewController {

    @IBOutlet var tapGestureRecognizer: UITapGestureRecognizer!
    override func viewDidLoad() {
        super.viewDidLoad()
        tapGestureRecognizer.delegate = self
    }

}

extension ViewController: UIGestureRecognizerDelegate {
    func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceive touch: UITouch) -> Bool {
        !(touch.view is UIControl)
    }
}
Paul B
  • 3,989
  • 33
  • 46