Skip to content

Instantly share code, notes, and snippets.

@andr3a88
Last active June 8, 2022 12:05
Show Gist options
  • Save andr3a88/d2353c24cd3c465e19535d762797c1ad to your computer and use it in GitHub Desktop.
Save andr3a88/d2353c24cd3c465e19535d762797c1ad to your computer and use it in GitHub Desktop.
Recognize long press gesture on a table view cell
class ViewController: UITableViewDataSource, UITableViewDelegate {
var longPressGesture: UILongPressGestureRecognizer!
override func viewDidLoad() {
super.viewDidLoad()
...
longPressGesture = UILongPressGestureRecognizer(target: self, action: #selector(longPressGestureRecognized(_:)))
self.tableView.addGestureRecognizer(longPressGesture)
...
}
@objc func longPressGestureRecognized(_ sender: UILongPressGestureRecognizer) {
if sender.state == .began {
if let section = self.tableView.indexPathForRow(at: sender.location(in: self.tableView))?.section {
let feedbackGenerator = UIImpactFeedbackGenerator(style: .medium)
feedbackGenerator.prepare()
feedbackGenerator.impactOccurred()
...
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment