0

I've a UITableView and I'd like to make an UIAlertView to appears on a UILongPressGestureRecognizer.

It works well right now, is that I've to lift my finger of the screen to see it appear.

Is there a way to make it appears after 0.5s (for example) even if the finger is still on the screen? With the property of the UILongPressGestureRecognizer?

EDIT:

class Settings: UIViewController , UITableViewDataSource, UITableViewDelegate {
override func viewDidLoad() {
    super.viewDidLoad()

    let longPress = UILongPressGestureRecognizer(target: self, action: #selector(Settings.deleteItem))
    self.tableView.addGestureRecognizer(longPress)
}

func deleteItem(longPress:UIGestureRecognizer) {
    if longPress.state == UIGestureRecognizerState.ended {
        let pressedLocation = longPress.location(in: self.tableView)
        if let pressedIndexPath = tableView.indexPathForRow(at: pressedLocation) { //Give the row where it's touched

            RowSelected = (actionList?[pressedIndexPath[1]])!
            print(RowSelected)
            longtouched = pressedIndexPath[1]
            let alert = UIAlertController(title: "What do you want to do?", message: "Select the action you want to do.", preferredStyle: UIAlertControllerStyle.alert)

            let Cancel = UIAlertAction(title: "Cancel", style: .default) { actio in
                print("Canceled")
            }


            let Delete = UIAlertAction(title: "Delete", style: .default) { actio in
                //DOING MY STUFF

            }

            let Modify = UIAlertAction(title: "Modify", style: .default) { actio in
                UserDefaults.standard.set(self.longtouched, forKey: "modify")
                self.performSegue(withIdentifier: "ToModify", sender: self)
            }
            alert.addAction(Modify)
            alert.addAction(Delete)
            alert.addAction(Cancel)
            self.present(alert, animated: true, completion: nil)
        }
    }
}
nyg
  • 2,380
  • 3
  • 25
  • 40
Hawkydoky
  • 194
  • 1
  • 15
  • @Anbu.Karthik : Just did, but I guess I figured out my problem. Just had to changed `if longPress.state == UIGestureRecognizerState.ended` to `if longPress.state == UIGestureRecognizerState.began`and it did the trick. I guess monday morning eyes help me to see that after hours of search into my code :/ Thanks anyway. – Hawkydoky Sep 04 '17 at 07:17
  • thats all my bro congrtz – Anbu.Karthik Sep 04 '17 at 07:18
  • Do you mean to show the alert 0.5s after the long press gesture triggers its selector? – thxou Sep 04 '17 at 07:23
  • No, I meant show the alert 0.5s after the finger touched the screen. For the duration, just had to add `longPress.minimumPressDuration = 0.5` or if added from the storyboard to configure it in the right panel. – Hawkydoky Sep 04 '17 at 07:34

1 Answers1

0

In fact I've seen the problem by myself.

If someone is facing the same problem, just changed if longPress.state == UIGestureRecognizerState.endedto if longPress.state == UIGestureRecognizerState.began It will do the trick.

Hawkydoky
  • 194
  • 1
  • 15