Swift: Long Press Gesture Recognizer – Detect taps and Long Press

Define two IBActions and set one Gesture Recognizer to each of them. This way you can perform two different actions for each gesture. You can set each Gesture Recognizer to different IBActions in the interface builder. @IBAction func tapped(sender: UITapGestureRecognizer) { println(“tapped”) //Your animation code. } @IBAction func longPressed(sender: UILongPressGestureRecognizer) { println(“longpressed”) //Different code } … Read more

UIButton Long Press Event

You can start off by creating and attaching the UILongPressGestureRecognizer instance to the button. UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)]; [self.button addGestureRecognizer:longPress]; [longPress release]; And then implement the method that handles the gesture – (void)longPress:(UILongPressGestureRecognizer*)gesture { if ( gesture.state == UIGestureRecognizerStateEnded ) { NSLog(@”Long Press”); } } Now this would be the basic approach. … Read more

Long press on UITableView

First add the long press gesture recognizer to the table view: UILongPressGestureRecognizer *lpgr = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPress:)]; lpgr.minimumPressDuration = 2.0; //seconds lpgr.delegate = self; [self.myTableView addGestureRecognizer:lpgr]; [lpgr release]; Then in the gesture handler: -(void)handleLongPress:(UILongPressGestureRecognizer *)gestureRecognizer { CGPoint p = [gestureRecognizer locationInView:self.myTableView]; NSIndexPath *indexPath = [self.myTableView indexPathForRowAtPoint:p]; if (indexPath == nil) { NSLog(@”long press on … Read more

Detect touch press vs long press vs movement?

This code can distinguish between click and movement (drag, scroll). In onTouchEvent set a flag isOnClick, and initial X, Y coordinates on ACTION_DOWN. Clear the flag on ACTION_MOVE (minding that unintentional movement is often detected which can be solved with a THRESHOLD const). private float mDownX; private float mDownY; private final float SCROLL_THRESHOLD = 10; … Read more