0

In my main viewcontroller, I have a uislider. and at the same time I assign UISwipeGestureRecognizer to the self.view as I want to detect gestures on the whole view.

The problem is whenever I slide the slider, the gesture recognizer captured it as well, and the slider behaviour is jerky and not nice.

Here is my code (gesture part)

UISwipeGestureRecognizer* singleSwipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeRight:)];
    [singleSwipeRight setDirection:(UISwipeGestureRecognizerDirectionRight)];
    singleSwipeRight.numberOfTouchesRequired = 1;
    singleSwipeRight.delegate = self;
    [self.view addGestureRecognizer: singleSwipeRight];

As I read in HERE, to prevent this, just implement the following delegate:

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {


    if ([touch.view isKindOfClass:[UISlider class]]) {
        // prevent recognizing touches on the slider
        //NSLog(@"no");
        return NO;
    }
    return YES;

}

But the funny thing is, this only work the 2nd time i slide the UISlider. The first time swipe ALWAYS goes to the gesturerecognizer handler.

Why? And how to solve this?

Thanks.

Community
  • 1
  • 1
GeneCode
  • 7,545
  • 8
  • 50
  • 85

2 Answers2

1

In your shouldReceiveTouch method, add this line:

NSLog(@"touch.view class = %@", [touch.view className]);

This will indicate the class name of what you're getting for the initial touch.

You should also check out the phase property; you may only want to return YES when the swipe has been completed (depending on how your app works).

Aaron Brager
  • 65,323
  • 19
  • 161
  • 287
  • Just checked it. log says UIView. btw, i used [touch.view class], there is no className property for that view. Not sure how to proceed though. The UISlider is inside another UIView that is in the self.view. Could this be the cause? If so, how do I rectify this? any idea? – GeneCode Jul 01 '12 at 15:23
  • Yup, so I removed the slider out of the child view and it works ok now. Would like to know if it is possible to check for a slider which is in another view though. Thanks anyways! – GeneCode Jul 01 '12 at 15:30
  • No. Actually it didn't work. No matter if the slider is inside a subview or not, the first view is still detected as a UIView eventhough I carefully touch on the UISlider. I'll investigate more... hopefully i can find something. – GeneCode Jul 02 '12 at 02:46
1

Add a pan gesture recognizer on slider and don't specify target nor selector (It prevents other gesture recognizers to work) Setting the property to NO allows the slider to track properly.

    UIPanGestureRecognizer* panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:nil action:nil];
[slider addGestureRecognizer:panGesture];
panGesture.cancelsTouchesInView = NO;

That's it!

Mayosse
  • 696
  • 1
  • 7
  • 16
  • @Mayosse Outstanding answer. My challenge was that the slider kept stopping before the user finished sliding across the screen. This solved the problem completely! Thank you! – kbpontius Mar 11 '16 at 19:11