15

I'm trying to create a sliding menu (kind of like in Facebook/Twitter apps) and my version successfully uses pan gestures for this effect. The class is called "SlideMenu".

I'm instantiating a SlideMenu in my ViewController, and then adding a bunch of UI elements as subviews on it, such as UISliders, UIButtons, etc.

The issue is that the pan gesture seems to interfere with the UISlider, as it will slide, but stop after a very short distance. I found a piece of code on an answer (Gesture problem: UISwipeGestureRecognizer + UISlider) however I am unsure on how to implement it or if it works with my design.

The code is this:

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
    if ([touch.view isKindOfClass:[UISlider class]]) {
        // prevent recognizing touches on the slider
        return NO;
    }
    return YES;
}

I tried adding it to my SliderMenu class and my ViewController, but no dice. Where does this go? What delegate do I have to set (if any?) Thanks

Community
  • 1
  • 1
user339946
  • 5,961
  • 9
  • 52
  • 97

2 Answers2

15

Basically, in whichever controller or view you are handling the gesture recognizer. When you create the gesture recognizer, you can set its delegate to some object (probably your view controller) and have this object implement the UIGestureRecognizerDelegate. One of the delegate call-backs is gestureRecognizer:shouldReceiveTouch, and so you just have to copy the code you placed above into your object's (view controller's) implementation.

deleterOfWorlds
  • 552
  • 5
  • 9
  • 1
    Thanks. The detail I was missing out on was setting the gesture's delegate to self. All good now! – user339946 Mar 08 '12 at 05:19
  • Great Answer!!, minusing – l0gg3r Feb 17 '14 at 14:27
  • Working for me. I but now my method that should be called on panning is not being called. How can i invoke this method? – Hassy Jun 26 '14 at 11:57
  • I have posted a question for this here is the [link](http://stackoverflow.com/questions/24429664/objective-c-uipangesturerecognizer-selector-not-invoked-with-uislider) – Hassy Jun 26 '14 at 11:58
0

I also had some issues with it, the source of my problem was that the

gestureRecogniser.cancelsTouchesInView

property is true by default, which caused a weird delay effect on my UISlider when tried to drag it. This solved

gestureRecogniser.cancelsTouchesInView = false

ps.: also make sure the delaysTouchesBegan, delaysTouchesEnded are set to false

Bence Pattogato
  • 3,752
  • 22
  • 30