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 8177035
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T23:24:24+00:00 2026-06-06T23:24:24+00:00

Something strange I encountered today while attaching same gesture recogniser to multiple image views.

  • 0

Something strange I encountered today while attaching same gesture recogniser to multiple image views. It gets attached to only the last one, in other words, it can be attached to only one view!

I had to create multiple gesture recognisers to meet my requirements.

Following is what I have done. Am I doing correct? Is that’s the only way to attach recognisers to the multiple imageviews?

Please note that I don’t want to use UITableView or UIVIew and put all imageviews in it and attach gesture recogniser to only UITableView or UIVIew. I have all image scattered and I have to detect which image is being dragged. Thanks.

[imgView1 setUserInteractionEnabled:YES];
[imgView1 setMultipleTouchEnabled:YES];

[imgView2 setUserInteractionEnabled:YES];
[imgView2 setMultipleTouchEnabled:YES];

[imgView3 setUserInteractionEnabled:YES];
[imgView3 setMultipleTouchEnabled:YES];

[imgView4 setUserInteractionEnabled:YES];
[imgView4 setMultipleTouchEnabled:YES];

[imgView5 setUserInteractionEnabled:YES];
[imgView5 setMultipleTouchEnabled:YES];

[imgView6 setUserInteractionEnabled:YES];
[imgView6 setMultipleTouchEnabled:YES];


//Attach gesture recognizer to each imagviews
gestureRecognizer1 = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(gestureHandler:)];
gestureRecognizer1.delegate = self;

gestureRecognizer2 = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(gestureHandler:)];
gestureRecognizer2.delegate = self;

gestureRecognizer3 = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(gestureHandler:)];
gestureRecognizer3.delegate = self;

gestureRecognizer4 = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(gestureHandler:)];
gestureRecognizer4.delegate = self;

gestureRecognizer5 = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(gestureHandler:)];
gestureRecognizer5.delegate = self;

gestureRecognizer6 = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(gestureHandler:)];
gestureRecognizer6.delegate = self;

[imgView1 addGestureRecognizer:gestureRecognizer1];
[imgView2 addGestureRecognizer:gestureRecognizer2];
[imgView3 addGestureRecognizer:gestureRecognizer3];
[imgView4 addGestureRecognizer:gestureRecognizer4];
[imgView5 addGestureRecognizer:gestureRecognizer5];
[imgView6 addGestureRecognizer:gestureRecognizer6];
  • 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-06T23:24:25+00:00Added an answer on June 6, 2026 at 11:24 pm

    Yes, one view per gesture recognizer. So if you want only one recognizer, put it on the superview, e.g.:

    UILongPressGestureRecognizer *gestureRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(gestureHandler:)];
    [self.view addGestureRecognizer:gestureRecognizer];
    

    And then, in your handler, you can:

    - (void)handleLongPress:(UILongPressGestureRecognizer *)sender
    {
        CGPoint location = [sender locationInView:self.view];
    
        if (sender.state == UIGestureRecognizerStateBegan)
        {
            for (UIView *view in self.view.subviews)
            {
                if ([view isKindOfClass:[UIImageView class]] && CGRectContainsPoint(view.frame, location))
                {
                    UIImageView *image = (UIImageView *) view;
    
                    // ok, now you know which image you received your long press for
                    // do whatever you wanted on it at this point
    
                    return;
                }
            }
        }
    }
    

    By the way, if you do that, you don’t need to worry about enabling user interaction on the images, either.

    Finally, you don’t need to worry about specifying your gesture recognizer’s delegate unless you’re going to conform to UIGestureRecognizerDelegate, which this isn’t. Also note that I’m using a local var for my recognizer because there’s no reason to hang onto it.

    Update:

    While the above code works fine, perhaps even better would be a custom long press gesture recognizer that would fail if the long press didn’t take place over an image (this way it’s more likely to play well in case you have other gesture recognizers taking place in your view). So:

    #import <UIKit/UIGestureRecognizerSubclass.h>
    
    @interface ImageLongPressGestureRecognizer : UILongPressGestureRecognizer
    @property (nonatomic, weak) UIImageView *imageview;
    @end
    
    @implementation ImageLongPressGestureRecognizer
    
    @synthesize imageview = _imageview;
    
    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
    {
        self.imageview = nil;
    
        [super touchesBegan:touches withEvent:event];
    
        CGPoint location = [self locationInView:self.view];
    
        for (UIView *view in self.view.subviews)
        {
            if ([view isKindOfClass:[UIImageView class]] && CGRectContainsPoint(view.frame, location))
            {
                self.imageview = (UIImageView *)view;
                return;
            }
        }
    
        self.state = UIGestureRecognizerStateFailed;
    }
    
    @end
    

    then create your gesture recognizer accordingly, using this new subclass:

    ImageLongPressGestureRecognizer *gestureRecognizer = [[ImageLongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPress:)];
    [self.view addGestureRecognizer:gestureRecognizer];
    

    and then, as a nice little benefit of this subclassing, your main gesture recognizer is simplified, namely:

    - (void)handleLongPress:(ImageLongPressGestureRecognizer *)sender
    {
        if (sender.state == UIGestureRecognizerStateBegan)
        {
            // you can now do whatever you want with sender.imageview, e.g. this makes it blink for you:
    
            [UIView animateWithDuration:0.5 
                             animations:^{
                                 sender.imageview.alpha = 0.0;
                             } completion:^(BOOL finished){
                                 [UIView animateWithDuration:0.5 
                                                  animations:^{
                                                      sender.imageview.alpha = 1.0;
                                                  } 
                                                  completion:nil];
                             }];
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Today I encountered something strange: I tried to put a utility method into an
I think I encountered something extraordinary strange in VS 2008. All the array values
I have encountered something very strange, simple WPF application <Window x:Class=ListBoxSelection.MainWindow xmlns=http://schemas.microsoft.com/winfx/2006/xaml/presentation xmlns:x=http://schemas.microsoft.com/winfx/2006/xaml Title=MainWindow
I encountered something strange in Backbone : All children class of a common parent
I'm trying to inflate an custom alertdialog and encountered something strange. layout = inflater.inflate(R.layout.call_or_sms_dialog,(ViewGroup)findViewById(R.id.contacts));
I've encountered something very strange, and things just don't add up. First of all,
The short story: I've encountered a strange problem with Locale::Maketext. Something turns german umlauts
I encountered a strange issue while coding a .dll file in C with gcc
I have just encountered something rather strange, I use the Zend Framework 1.10 with
I'm working with MVC recently and I've encountered a strange problem while trying to

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.