UILongPressGestureRecognizer gets called twice when pressing down

Cover Image for UILongPressGestureRecognizer gets called twice when pressing down
Matheus Mello
Matheus Mello
published a few days ago. updated a few hours ago

Fixing the Issue of UILongPressGestureRecognizer Getting Called Twice

šŸ‘‹ Hey there, tech enthusiasts! In today's blog post, we're going to tackle a common issue that some iOS developers might encounter when using the UILongPressGestureRecognizer. šŸ“±šŸ’„ Specifically, we'll address why this recognizer's handler method gets called twice instead of just once when you press down for longer than the specified duration. šŸ˜®

Before diving into the solutions, let's quickly go over the code snippet provided for some context:

UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]
                                             initWithTarget:self 
                                             action:@selector(handleLongPress:)];
longPress.minimumPressDuration = 2.0;
[self addGestureRecognizer:longPress];
[longPress release];

The code above sets up a UILongPressGestureRecognizer instance, longPress, and assigns it a target and an action using the initWithTarget:action: method. The minimumPressDuration property is also set to 2.0 seconds, indicating that the gesture should be recognized after holding down for at least that duration. Finally, the gesture recognizer is added to a view and released.

The handler method for the long press is defined as follows:

-(void)handleLongPress:(UILongPressGestureRecognizer*)recognizer{
    NSLog(@"double oo");
}

Now, let's address the question at hand: Why is the handleLongPress: method getting called twice when you press down for longer than 2 seconds? And, more importantly, how can you fix this behavior? šŸ¤”

Understanding the Issue

The reason for this behavior is not a bug or a mistake in your code but rather an intended feature of UILongPressGestureRecognizer. When you press down and hold your finger for longer than the specified minimumPressDuration, the recognizer initially fires one gesture event for the "began" state and then continuously repeats the "changed" state events at a predefined interval. šŸ”„

This "began" event is the first time the gesture is recognized, while the "changed" events are triggered continuously until you release your finger. Therefore, when you hold down for a longer duration, both the "began" event and multiple "changed" events occur, resulting in the handler method being called multiple times. ā°

Solving the Issue

Now that we understand why the handleLongPress: method is triggered twice, let's look at two possible solutions to address this issue and ensure that the handler only runs once.

Solution 1: Use Gesture Recognizer State

One way to handle this is by checking the state property of UILongPressGestureRecognizer within your method implementation. By doing so, you can determine when the gesture has transitioned from the "began" state to the "changed" state, and run your code only once at that point. Here's an updated version of the handleLongPress: method to demonstrate this:

-(void)handleLongPress:(UILongPressGestureRecognizer*)recognizer{
    if (recognizer.state == UIGestureRecognizerStateBegan) {
        NSLog(@"double oo");
        // Perform your logic here for the "began" state event
    }
}

With this modification, the code within the if statement will execute only when the gesture recognizer transitions to the "began" state, preventing duplicate execution.

Solution 2: Use Gesture Recognizer Delegate

Another approach is to make use of the UIGestureRecognizerDelegate protocol to control the execution of the handler method. By implementing the shouldRecognizeSimultaneouslyWithGestureRecognizer: method and returning NO for the UILongPressGestureRecognizer, you can prevent duplicate recognition. Here's an example of how to implement this solution:

@interface YourViewController () <UIGestureRecognizerDelegate>
@end

@implementation YourViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]
                                               initWithTarget:self 
                                               action:@selector(handleLongPress:)];
    longPress.minimumPressDuration = 2.0;
    longPress.delegate = self; // Set the delegate to self
    [self addGestureRecognizer:longPress];
    [longPress release];
}

// Implement UIGestureRecognizerDelegate method
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer 
shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
    return NO;
}

-(void)handleLongPress:(UILongPressGestureRecognizer*)recognizer{
    NSLog(@"double oo");
    // Perform your logic here
}

@end

By setting the delegate property of UILongPressGestureRecognizer to self and implementing the shouldRecognizeSimultaneouslyWithGestureRecognizer: method, we make sure that the handler method is called only once.

It's Time to Get It Right!

And there you have it, folks! Two easy-to-implement solutions to fix the issue of UILongPressGestureRecognizer getting called twice instead of once: using the gesture recognizer state or leveraging the gesture recognizer delegate. šŸ™Œ

Now it's up to you to choose your preferred solution and get your app behaving the way you want it to. Remember, understanding the problem is key to finding the right solution, and hopefully, this blog post has shed some light on the issue and offered you tools to fix it. šŸ’”šŸ”§

If you have any questions, further issues, or suggestions, please feel free to reach out and engage with us in the comments section below. We'd love to hear from you! šŸ“šŸ—£ļø

Happy coding, and until next time! šŸš€āœØ


More Stories

Cover Image for How can I echo a newline in a batch file?

How can I echo a newline in a batch file?

updated a few hours ago
batch-filenewlinewindows

šŸ”„ šŸ’» šŸ†’ Title: "Getting a Fresh Start: How to Echo a Newline in a Batch File" Introduction: Hey there, tech enthusiasts! Have you ever found yourself in a sticky situation with your batch file output? We've got your back! In this exciting blog post, we

Matheus Mello
Matheus Mello
Cover Image for How do I run Redis on Windows?

How do I run Redis on Windows?

updated a few hours ago
rediswindows

# Running Redis on Windows: Easy Solutions for Redis Enthusiasts! šŸš€ Redis is a powerful and popular in-memory data structure store that offers blazing-fast performance and versatility. However, if you're a Windows user, you might have stumbled upon the c

Matheus Mello
Matheus Mello
Cover Image for Best way to strip punctuation from a string

Best way to strip punctuation from a string

updated a few hours ago
punctuationpythonstring

# The Art of Stripping Punctuation: Simplifying Your Strings šŸ’„āœ‚ļø Are you tired of dealing with pesky punctuation marks that cause chaos in your strings? Have no fear, for we have a solution that will strip those buggers away and leave your texts clean an

Matheus Mello
Matheus Mello
Cover Image for Purge or recreate a Ruby on Rails database

Purge or recreate a Ruby on Rails database

updated a few hours ago
rakeruby-on-railsruby-on-rails-3

# Purge or Recreate a Ruby on Rails Database: A Simple Guide šŸš€ So, you have a Ruby on Rails database that's full of data, and you're now considering deleting everything and starting from scratch. Should you purge the database or recreate it? šŸ¤” Well, my

Matheus Mello
Matheus Mello