6

I'm trying to call an alert box when I touch and hold down an image for 2 seconds. Here's what I got so far:

- (void)viewDidLoad
{
    [super viewDidLoad];

    UILongPressGestureRecognizer *tapAndHoldGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapAndHoldGesture:)];
    tapAndHoldGesture.minimumPressDuration = 0.1;
    tapAndHoldGesture.allowableMovement = 600;
    [self.view addGestureRecognizer:tapAndHoldGesture]; 
}

- (void) handleTapAndHoldGesture:(UILongPressGestureRecognizer *)gestureRecognizer{
    if (gestureRecognizer.state != UIGestureRecognizerStateEnded) {
        return;
    }
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Gesture:" message:@"hold it" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [alert show];
}

Not sure if this effects anything, but the Image View is programmatically created later and not on load. Thank you in advance as any help is appreciated..

Also, I've looked at the following links:

Long press gesture on UICollectionViewCell

Long press gesture recognizer on UIButton?

Apple Link 1

Apple Link 2

Community
  • 1
  • 1
0--
  • 211
  • 1
  • 5
  • 15

2 Answers2

8
-(void)viewDidLoad
{
    [super viewDidLoad];
    [self setupGesture];
}

-(void) setupGesture
{
    UILongPressGestureRecognizer *lpHandler = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleHoldGesture:)];
    lpHandler.minimumPressDuration = 1; //seconds
    lpHandler.delegate = self;
    //myUIImageViewInstance - replace for your instance/variable name
    [**myUIImageViewInstance** addGestureRecognizer:lpHandler];
}

- (void) handleHoldGesture:(UILongPressGestureRecognizer *)gesture
{
   if(UIGestureRecognizerStateBegan == gesture.state)
   {
        // Called on start of gesture, do work here
   }

   if(UIGestureRecognizerStateChanged == gesture.state)
   {
        // Do repeated work here (repeats continuously) while finger is down
   }

   if(UIGestureRecognizerStateEnded == gesture.state)
   {
        // Do end work here when finger is lifted
   }

}
Kamen Dobrev
  • 1,232
  • 13
  • 18
gabriel
  • 2,321
  • 4
  • 18
  • 23
  • Still doesn't work for me...Does it matter that my image is 22x22? – 0-- Jul 15 '14 at 14:54
  • I don't think so. Check the propriety userInteractionEnabled, should be YES (true) Based on the Class Reference: userInteractionEnabled A Boolean value that determines whether user events are ignored and removed from the event queue. @property(nonatomic, getter=isUserInteractionEnabled) BOOL userInteractionEnabled Discussion This property is inherited from the UIView parent class. This class changes the default value of this property to NO. – gabriel Jul 15 '14 at 15:12
  • worked for me without adding the line *lpHandler.delegate = self;* – LuAndre Jan 29 '17 at 01:20
  • Doesn't work for me, Better follow this solution : https://stackoverflow.com/a/34548629/4994239 – Moosa Baloch Apr 26 '18 at 10:45
3

UIImageViews by default have userInteractionEnabled = NO. If you're adding your gesture recognizer to an instance of UIImageView, make sure you set it back to YES: myImageView.userInteractionEnabled = YES

John Grant
  • 1,619
  • 10
  • 8