Kenny
Kenny

Reputation: 1121

UISwipeGestureRecognizer interferes with slider

I have a view in an iOS application (Obj-C) which has an image view in the centre, and immediately below that a slider. The image view shows album artwork, and the slider can be used to adjust the now-playing track position.

There is also a pair of left and right Swipe Gesture Recognizers. These are used to skip to the next or previous tracks.

The problem is that the swipe gesture recognizers seem to over-ride the users moving the slider thumb. In my gesture recognizer code I check that the point touched was inside the image view, but it still stops the slider from being moved. (The thumb moves, but jumps back to it's original position when you remove your finger).

This is the code I use to reject the gesture if it's not inside the image view.

- (IBAction)swipeLeftGestureAction:(UISwipeGestureRecognizer *)sender {
    // Get the location of the gesture.
    CGPoint tapPoint = [sender locationInView:_artworkImageView];

    // Make sure tap was INSIDE the artwork image frame.
    if( (tapPoint.x <0)
       || (tapPoint.y < 0 )
       || (tapPoint.x > _artworkImageView.frame.size.width)
       || (tapPoint.y>_artworkImageView.frame.size.height))
    {
        NSLog(@"Outside!");
        return;
    }
    NSLog(@"Swipe LEFT");
    [_mediaController skipNext];
}

So my question is, how do I limit the gesture to work ONLY when swiped across the image view?

Upvotes: 1

Views: 76

Answers (3)

Artal
Artal

Reputation: 9143

Try to put the code that restricts the gesture's area in gestureRecognizer:shouldReceiveTouch: and return NO in case you don't want the gesture to receive this touch. It should prevent the gesture from over taking the slider interaction.

Upvotes: 2

johnpatrickmorgan
johnpatrickmorgan

Reputation: 2372

If you're only interested in swipes that are inside the image view, then you should add the swipe gesture recognizer to the image view instead of adding it to your entire view. Then you won't need any special logic.

Upvotes: 1

Dan Kuida
Dan Kuida

Reputation: 1057

There is a dedicated method for checking if a point is inside a view

BOOL isPointInsideView = [_artworkImageViewpointInside:tapPoint  withEvent:nil];

But I think what is happening is that if you will look at the tapPoint is that it actually outside of your imageView And if your slider is really close to the imageView so the slider captures the movement, what you should do is check on the slider if it is intended for the slider and propagate on to the imageView if needed Or inherit the UISlider and reduce its response rect

Upvotes: -1

Related Questions