How to implement Swipe and Hold gesture recognizer in Objective-C?

717 Views Asked by At

I am working on an application which works like a remote control of different devices like Game Console, Set-top Box, etc.

Application contains different type of controls like gesture and buttons to perform events as remote.

Single gesture view contains multiple gesture recognizer as follow:

  1. 1 finger Tap using UITapGestureRecognizer

  2. 2 finger Tap using UITapGestureRecognizer

  3. 1 finger swipe using UISwipeGestureRecognizer

  4. 2 finger swipe using UISwipeGestureRecognizer

My requirement is to implement 1 finger Swipe gesture recognizer along with hold event. So, that will continue my swipe action until user will not remove its finger from the view.

I have tried UILongPressGestureRecognizer after UISwipeGestureRecognizer but this didn’t work for me because it is executing along with Swipe movement. And I want this after Swipe end, but finger will not remove.

My code snippet :

-(void) gestureView:(UIView*)viewGesture {
    UISwipeGestureRecognizer *swipeGestureSingleRight;
    UISwipeGestureRecognizer *swipeGestureSingleLeft;
    UISwipeGestureRecognizer *swipeGestureSingleUp;
    UISwipeGestureRecognizer *swipeGestureSingleDown;

    swipeGestureSingleUp = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeGestureSingle_Handle:)];
    [swipeGestureSingleUp setDelegate:self];
    [swipeGestureSingleUp setDirection:(UISwipeGestureRecognizerDirectionUp)];
    [swipeGestureSingleUp setNumberOfTouchesRequired:1];
    [viewGesture addGestureRecognizer:swipeGestureSingleUp];

    swipeGestureSingleDown = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeGestureSingle_Handle:)];
    [swipeGestureSingleDown setDelegate:self];
    [swipeGestureSingleDown setDirection:(UISwipeGestureRecognizerDirectionDown)];
    [swipeGestureSingleDown setNumberOfTouchesRequired:1];
    [viewGesture addGestureRecognizer:swipeGestureSingleDown];

    swipeGestureSingleRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeGestureSingle_Handle:)];
    [swipeGestureSingleRight setDelegate:self];
    [swipeGestureSingleRight setDirection:(UISwipeGestureRecognizerDirectionRight)];
    [swipeGestureSingleRight setNumberOfTouchesRequired:1];
    [viewGesture addGestureRecognizer:swipeGestureSingleRight];

    swipeGestureSingleLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeGestureSingle_Handle:)];
    [swipeGestureSingleLeft setDelegate:self];
    [swipeGestureSingleLeft setDirection:(UISwipeGestureRecognizerDirectionLeft)];
    [swipeGestureSingleLeft setNumberOfTouchesRequired:1];
    [viewGesture addGestureRecognizer:swipeGestureSingleLeft];

    longPressGestureOnSwipe = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressGesture_Handle:)];
    longPressGestureOnSwipe.minimumPressDuration = 0.03;
    longPressGestureOnSwipe.delegate = self;
    [viewGesture addGestureRecognizer:longPressGestureOnSwipe];
    isSwipeEnd = false;
    [longPressGestureOnSwipe setEnabled:isSwipeEnd];
}

- (void)swipeGestureSingle_Handle:(UISwipeGestureRecognizer*)sender {
    if (sender.state == UIGestureRecognizerStateEnded) {
        singleTouchSwipeSenderDirection = sender.direction;
        isSwipeEnd = true;
        [longPressGestureOnSwipe setEnabled:isSwipeEnd];
    }
}

-(void)longPressGesture_Handle:(UILongPressGestureRecognizer*)sender {
    if (sender.state == UIGestureRecognizerStateEnded) {
        isSwipeEnd = false;
        [longPressGestureOnSwipe setEnabled:isSwipeEnd];
    }
}

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
    return YES;
}

- (void)touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event {
    if (isSwipeEnd) {
        [super touchesBegan:touches withEvent:event];
    } else {
        return;
    }
}

Please suggest me correction or solution for my requirement.

Thanks in advance!

0

There are 0 best solutions below