How to implement a tap and hold on an UIImageView?
Asked Answered
D

2

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

Duteous answered 15/7, 2014 at 13:29 Comment(2)
Where you are adding gesture to imageView?. I can see only the handler methodAdventitia
My mistake...In the viewDidLoad. Thanks.Duteous
B
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
   }

}
Bayonet answered 15/7, 2014 at 13:45 Comment(4)
Still doesn't work for me...Does it matter that my image is 22x22?Duteous
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.Bayonet
worked for me without adding the line lpHandler.delegate = self;Fellow
Doesn't work for me, Better follow this solution : https://mcmap.net/q/555704/-button-tap-and-long-press-gestureInterregnum
R
4

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

Reword answered 15/7, 2014 at 15:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.