J'ai une table qui a un identificateur de geste de pression longue qui exécute le code en fonction de la ligne de table sélectionnée.
Le problème que j'ai, c'est que je dois actuellement appuyer sur la ligne que je veux, puis appuyer longuement.
Comment puis-je faire en sorte que le tableau sélectionne la ligne sur laquelle j'appuie longuement sans avoir à appuyer sur pour la sélectionner en premier?
Le code suivant fonctionne bien pour moi:
Ajoutez un identificateur de geste de pression longue dans viewDidLoad:
// tapRecognizer, placed in viewDidLoad
let longPressRecognizer = UILongPressGestureRecognizer(target: self, action: "longPress:")
self.view.addGestureRecognizer(longPressRecognizer)
La méthode invoquée par la pression longue ressemble alors à ceci:
//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
}
}
Swift 4
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 == .ended {
let touchPoint = gestureRecognizer.location(in: self.tblMessage)
if let indexPath = tblMessage.indexPathForRow(at: touchPoint) {
}
}
}
Swift
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
}
}
}
Objectif - 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
}
}
}
Fonction Swift 3:
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
}
}
viewDidLoad:
let longPressGesture:UILongPressGestureRecognizer = UILongPressGestureRecognizer(target: self, action: #selector(YourViewController.handleLongPress(_:)))
longPressGesture.minimumPressDuration = 1.0 // 1 second press
longPressGesture.delegate = self
self.tableView.addGestureRecognizer(longPressGesture)
Swift 4
let longPressRecognizer = UILongPressGestureRecognizer(target: self, action: #selector(longPressed(sender:)))
self.view.addGestureRecognizer(longPressRecognizer)
// MARQUE: Actions
@objc func longPressed(sender: UILongPressGestureRecognizer) {
if sender.state == UIGestureRecognizerState.began {
let touchPoint = sender.location(in: self.tableView)
if let indexPath = tableView.indexPathForRow(at: touchPoint) {
print("Long pressed row: \(indexPath.row)")
}
}
}
Pour la version 3 de Swift
func longPress(_ longPressGestureRecognizer: UILongPressGestureRecognizer) {
if longPressGestureRecognizer.state == UIGestureRecognizerState.began {
let touchPoint = longPressGestureRecognizer.location(in: self.view)
if let indexPath = notificationTabelView.indexPathForRow(at: touchPoint) {
print("indexPath=\(indexPath)")
// your code here, get the row for the indexPath or do whatever you want
}
}
}
Dans votre fonction viewDidLoad
let longPressGesture:UILongPressGestureRecognizer = UILongPressGestureRecognizer(target: self, action: #selector(EmployeeNotificationViewController.longPress(_:)))
longPressGesture.minimumPressDuration = 1.0 // 1 second press
longPressGesture.delegate = self as? UIGestureRecognizerDelegate
self.notificationTabelView.addGestureRecognizer(longPressGesture)
Pour éviter cela, vous pouvez ajouter le UILongPressGestureRecognizer
à l'intérieur du cellForRowAtIndexPath
au lieu de didSelectRowAtIndexPath
En utilisant IBAction, vous pouvez faire (pour une CollectionView):
@IBAction func handleLongPress(sender: AnyObject) {
if sender.state == UIGestureRecognizerState.Began
{
let position = sender.locationInView(sender.view)
if let indexPath : NSIndexPath = ((sender.view as! UICollectionView).indexPathForItemAtPoint(position))!{
print("You holding cell #\(indexPath.item)!")
}
}
}
N'oubliez pas de créer un lien avec votre reconnaissance de gestes Long Press.
let longPressGesture = UILongPressGestureRecognizer(target: self, action: (#selector(YourCustomeTableCell.longTap)))
self.addGestureRecognizer(longPressGesture)
func longTap(){
print("Long tap")
}