Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8944007
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T11:55:37+00:00 2026-06-15T11:55:37+00:00

I’m trying to implement in my ViewController( which has a tableView) two gesture recognisers

  • 0

I’m trying to implement in my ViewController( which has a tableView) two gesture recognisers that have to work one after another. The first one is a swipe down gesture and the second a long press gesture.

Here’s my code modified with @sergio suggestions

    - (void)viewDidLoad
    {
        [super viewDidLoad];

        swipeDown = [[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeDownAction)] autorelease];

        longPress = [[[CustomLongPress alloc]initWithTarget:self action:@selector(longPressAction)] autorelease];


        longPress.minimumPressDuration = 2;

        swipeDown.numberOfTouchesRequired = 1;

        swipeDown.direction = UISwipeGestureRecognizerDirectionDown;


       swipeDown.delegate = self ;

        longPress.delegate = self ;

        [myTableView addGestureRecognizer:swipeDown];

        [myTableView addGestureRecognizer:longPress];





  }


    -(void)swipeDownAction {

        _methodHasBeenCalled = YES;    // bool @property declared in .h

        NSLog(@"Swipe down detected");


    }



    -(void)longPressAction {

        NSLog(@"long press detected");
    }

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

And my UILongPressGestureRecognizer subclass:

#import "CustomLongPress.h"
#import "ViewController.h"


@interface CustomLongPress()
{

    ViewController *vc;
}

@end


@implementation CustomLongPress
-(id)initWithTarget:(id)target action:(SEL)action controller:(ViewController *)viewCon
{
    self = [super initWithTarget:target action:action];

    if (self) {

        vc = viewCon;

    }

    return self;
}


-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
     NSLog(vc.methodHasBeenCalled ? @"Yes" : @"No");

    if (vc.methodHasBeenCalled) {

        [super touchesBegan:touches withEvent:event];
    }
}

Unfortunately, I still get only the log from swipeDown but no log when it comes to longPress

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T11:55:38+00:00Added an answer on June 15, 2026 at 11:55 am

    For that you need to create you own custom gesture recognizer. The best way would be for you to subclass UILongPressGestureRecognizer and make it “accept” the long press only after the swipe has ended. E.g., in the touchesBegan method

    - (void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event {
       <if swipe recognizer action method has been called>
           [super touchesBegan:touches withEvent:event];
       }
    }
    

    In this way, both gesture recognizers would try to recognize the gesture: the swipe recognizer would do so immediately; while the custom recognizer would “wait” for the swipe recognizer to have fired.

    An easy way to implement the condition <if swipe recognizer action method has been called> would be setting a global flag in your swipe action (you set it when the swipe action is executed; then the custom recognizer reads its value). This is easy, by far not the best implementation you can find.

    Another approach would be relying on the requiresGestureRecognizerToFail to chain 3 standard gesture recognizers, let’s call them A, B, and C, where:

    1. A is a UISwipeGestureRecognizer;
    2. C is a UILongPressGestureRecognizer;
    3. B is any gesture recognizer.

    You configure them like this:

    C --> B --> A
    

    whereby x --> y denotes that x requires y to fail. Thus, you would have that C (your long press gesture recognizer) will require the B GR to fail and B requires A (your swipe recognizer) to fail; B will fail as soon as A will recognized the swipe; once B fails, C will be allowed to recognize the long press if any.

    EDIT:

    after reading your comment, would you mind trying this and see if it helps out:

    1. remove your overridden touchesBegan and replace it by:

      - (void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event {
      }
      
    2. then define:

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

    If this does not work, then make your custom gesture recognizer inherit from a generic gesture recognizer and

    1. leave touchesBegan: in place and replace touchesMoved with:

      - (void)touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event {
      
        self.lastTouch = [touches anyObject];
        if (vc.methodHasBeenCalled && self.gestureNotBegunYet == YES) {
          self.gestureNotBegunYet = NO;
          [self performSelector:@selector(recognizeLongPress) withObject:nil afterDelay:1.0];
      
        } else {
           return;
        }
      }
      

    and add:

    - (float)travelledDistance {
      CGPoint currentLocation = [self.lastTouch locationInView:self.view.superview];
      return sqrt(pow((currentLocation.x - self.initialLocation.x), 2.0) +
                pow((currentLocation.y - self.initialLocation.y), 2.0));
    }
    
    - (void)fail {
      self.gestureNotBegunYet = YES;
      [NSObject cancelPreviousPerformRequestsWithTarget:self];
    
    }
    
    - (void)recognizeLongPress {
    
      if ([self travelledDistance] < kTapDragThreshold) {
        self.longPressed = YES;
        self.state = UIGestureRecognizerStateChanged;
      } else {
            [self fail];
            self.state = UIGestureRecognizerStateFailed;
      }
    }
    
    - (void)touchesEnded:(NSSet*)touches withEvent:(UIEvent*)event {
      [self fail];
      [super touchesEnded:touches withEvent:event];
    }
    
    - (void)touchesCancelled:(NSSet*)touches withEvent:(UIEvent*)event {
      [self fail];
      [super touchesCancelled:touches withEvent:event];
    }
    

    You will need to define in .h

    @property(nonatomic) CGPoint initialLocation;
    @property(nonatomic, retain) UITouch* lastTouch;
    @property(nonatomic) BOOL gestureNotBegunYet;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have an array which has BIG numbers and small numbers in it. I
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I've got a string that has curly quotes in it. I'd like to replace
I have a small JavaScript validation script that validates inputs based on Regex. I
I have a French site that I want to parse, but am running into
I have an autohotkey script which looks up a word in a bilingual dictionary

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.