I have a table which has a long press gesture recogniser that runs code depending on what table row is selected.
The trouble I'm having is that I currently have to tap the row I want then do the long press.
How can I make the table select the row that I am long pressing without having to tap to select it first?
Swift 4 & 5
override func viewDidLoad() { super.viewDidLoad() setupLongPressGesture() } func setupLongPressGesture() { let longPressGesture:UILongPressGestureRecognizer = UILongPressGestureRecognizer(target: self, action: #selector(self.handleLongPress)) longPressGesture.minimumPressDuration = 1.0 // 1 second press longPressGesture.delegate = self self.tblMessage.addGestureRecognizer(longPressGesture) } @objc func handleLongPress(_ gestureRecognizer: UILongPressGestureRecognizer){ if gestureRecognizer.state == .began { let touchPoint = gestureRecognizer.location(in: self.tblMessage) if let indexPath = tblMessage.indexPathForRow(at: touchPoint) { } } }
Swift 3
override func viewDidLoad() { super.viewDidLoad() setupLongPressGesture() } func setupLongPressGesture() { let longPressGesture:UILongPressGestureRecognizer = UILongPressGestureRecognizer(target: self, action: #selector(YourViewController.handleLongPress(_:))) longPressGesture.minimumPressDuration = 1.0 // 1 second press longPressGesture.delegate = self self.tblMessage.addGestureRecognizer(longPressGesture) } func handleLongPress(_ longPressGestureRecognizer: UILongPressGestureRecognizer) { if longPressGestureRecognizer.state == UIGestureRecognizerState.Began { let touchPoint = longPressGestureRecognizer.locationInView(self.view) if let indexPath = tableView.indexPathForRowAtPoint(touchPoint) { // your code here, get the row for the indexPath or do whatever you want } } }
Objective - C
UILongPressGestureRecognizer* longPressRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(onLongPress:)]; [self.tableView addGestureRecognizer:longPressRecognizer]; -(void)onLongPress:(UILongPressGestureRecognizer*)pGesture { if (pGesture.state == UIGestureRecognizerStateRecognized) { //Do something to tell the user! } if (pGesture.state == UIGestureRecognizerStateEnded) { UITableView* tableView = (UITableView*)self.view; CGPoint touchPoint = [pGesture locationInView:self.view]; NSIndexPath* row = [tableView indexPathForRowAtPoint:touchPoint]; if (row != nil) { //Handle the long press on row } } }
The following code works fine for me:
Add a long press gesture recognizer in viewDidLoad:
// tapRecognizer, placed in viewDidLoad let longPressRecognizer = UILongPressGestureRecognizer(target: self, action: "longPress:") self.view.addGestureRecognizer(longPressRecognizer)
Then the method invoke by the long press looks like this:
//Called, when long press occurred func longPress(longPressGestureRecognizer: UILongPressGestureRecognizer) { if longPressGestureRecognizer.state == UIGestureRecognizerState.Began { let touchPoint = longPressGestureRecognizer.locationInView(self.view) if let indexPath = tableView.indexPathForRowAtPoint(touchPoint) { // your code here, get the row for the indexPath or do whatever you want } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With