简体   繁体   中英

UILongPressGestureRecognizer does not do anything

I wanted to have a UI button respond only if it is being held for more than a set number of seconds. So I used UILongPressGestureRecognizer as so:

import UIKit

class ViewController: UIViewController {

@IBOutlet weak var holdButton: UIButton!

@IBAction func holdButtonPressed(_ sender: Any) {
    let recognizer = UILongPressGestureRecognizer(target: self, action: #selector(longPressHappened))
    recognizer.minimumPressDuration = 2.0
    view.addGestureRecognizer(recognizer)
}

and the handler

@objc func longPressHappened(gestureReconizer: UILongPressGestureRecognizer){
    holdButton.backgroundColor = #colorLiteral(red: 0.7254902124, green: 0.4784313738, blue: 0.09803921729, alpha: 1)
    DispatchQueue.main.async {
         print ("Sucess")
    }
   
}

As you can see, i have used DispatchQueue and tried to change the color of the button but neither are working. Can someone please tell me why?

Update :- I am confused with implementing the methods given in the answer so i thought i will give my full code again

import UIKit

class ViewController: UIViewController {

@IBOutlet weak var holdButton: UIButton! {
didSet {
    
         let recognizer = UILongPressGestureRecognizer(target: self,action: #selector(longPressHappened))
         recognizer.minimumPressDuration = 2.0
         holdButton.addGestureRecognizer(recognizer)
     }
 }

override func viewDidLoad() {
    super.viewDidLoad()
    
    holdButton.layer.cornerRadius = 150
    holdButton.layer.borderWidth = 1.0
    holdButton.layer.borderColor = #colorLiteral(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0)
    holdButton.clipsToBounds = true
}


@objc func longPressHappened(gestureReconizer: UILongPressGestureRecognizer){
    holdButton.backgroundColor = #colorLiteral(red: 0.7254902124, green: 0.4784313738, blue: 0.09803921729, alpha: 1)
    DispatchQueue.main.async {
         print ("Sucess")
    }
   
}

}

You just need to create custom button using UIView. Add a long press gesture to that view and upon required time triggered the delegate/Closures.

func addLongPressGesture() {
    let recognizer = UILongPressGestureRecognizer(target: self, action: #selector(handleLongPress(_:)))
    recognizer.minimumPressDuration = 3.0 // Duration
    customButton.addGestureRecognizer(recognizer)
}

@objc
func handleLongPress(_ gestureRecognizer: UILongPressGestureRecognizer) {
    if gestureRecognizer.state == .began {
        // Perform your functionality here
    }
}

You need to add gesture to button instead of view

@IBOutlet weak var holdButton: UIButton! {
      didSet {
            let recognizer = UILongPressGestureRecognizer(target: self,action: #selector(longPressHappened))
            recognizer.minimumPressDuration = 2.0
            holdButton.addGestureRecognizer(recognizer)
        }
    }

Add the gesture in your viewDidLoad instead:

override func viewDidLoad() {
    super.viewDidLoad()
    //...
    let recognizer = UILongPressGestureRecognizer(target: self,action: #selector(longPressHappened))
    recognizer.minimumPressDuration = 2.0
    holdButton.addGestureRecognizer(recognizer)
}

Whole isue is because, you are using UIBUtton for gesture recogniztion. you have to use Uiview for UILongPressGestureRecognizer .

If you want the animations like UIButton then you have to use manual animations or you can have ready to made code fro internet.

If you need further help, you can ask in comment

So I was looking at other answers and I found that all of them were in Objective-C which was why i posted this question... So because the answers were not working for me i used Swiftify to convert the code from This Question , and after some modification, it worked.

Here is the code snippet

    override func viewDidLoad() {
        super.viewDidLoad()
        //...
       let longPress_gr = UILongPressGestureRecognizer(target: self, action: #selector(doAction(_:)))
       longPress_gr.minimumPressDuration = 2 // triggers the action after 2 seconds of press
       holdButton.addGestureRecognizer(longPress_gr)
}

and then the objective-c function to make sure the code only gets triggered once after the long press happened

@objc func doAction(_ recognizer: UILongPressGestureRecognizer?) {

    if recognizer?.state == .began {
        print("sucess")
    }
}

(However, I cant understand the difference between this answer and some of the answers given above... can someone comment on how it work)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM