Last active
June 8, 2022 12:05
-
-
Save andr3a88/d2353c24cd3c465e19535d762797c1ad to your computer and use it in GitHub Desktop.
Recognize long press gesture on a table view cell
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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