0
votes

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.

2

2 Answers

1
votes

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).

1
votes

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!