UITapGestureRecognizer interferes with UISlider

I am having a gesture related problem somewhat similar to: gesture-problem-uiswipegesturerecognizer-uislider

But I am fast programming, so I need a quick solution.

What's going on in my project: I have ViewController

one on which clicking on the screen by the user will perform a segue. But I have UISlider

on this view manager, and when the user "releases" the slider, it sometimes (why sometimes and not always confuses me) is recognized as a tap on the screen.

So, I understand that I need to prevent the "sees / recognizes" trait recognizer affects UISlider

.

But how can I prevent this? (in swift 2.0, using Xcode7, but I also understand this if you are using the earlier swift coding in the answer) I am new to coding in swift. Hope someone can help!

Here is the code in the viewcontroller:

// The slider 
@IBOutlet weak var sliderValue: UISlider!

@IBAction func sliderValueChanged(sender: UISlider) {
// Do stuff
}

// UITapGestureRecognizer
override func viewDidLoad() {
    super.viewDidLoad()

let touch = UITapGestureRecognizer(target: self, action: "touched:")
        self.view.addGestureRecognizer(touch)
}

// Perform Segue
func touched (_: UIGestureRecognizer) {

    //Segue to another viewcontroller
    performSegueWithIdentifier("nice", sender: self)

}

      

(EDIT :) I've updated my code with the information I found here on stackoverflow. I added UIGestureRecognizerDelegate

:

    class LampOn: UIViewController, UIGestureRecognizerDelegate {...}

      

And I added shouldReceiveTouch

:

func gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldReceiveTouch touch: UITouch) -> Bool {

    gestureRecognizer.delegate = self
    if (touch.view == sliderValue){
        print("touching slider")
        return false
    }
    else{
        print("touching elsewhere")
        return true
    }

}

      

But the "shouldReceiveTouch" function is never called from the console. So what am I missing? Have I configured the delegate correctly?

+3


source to share


1 answer


The shouldReceiveTouch function is never called because you are setting the delegate inside the shouldReceiveTouch function. The delegate must be self-configured before the function can be called.

What you need to do is set a delegate inside viewDidLoad () and you should be fine.



override func viewDidLoad() {
  super.viewDidLoad()

  let touch = UITapGestureRecognizer(target: self, action: "touched:")
    self.view.addGestureRecognizer(touch)
    touch.delegate = self
}

      

+1


source







All Articles