UILongPressGestureRecognizer 不做任何事情

UILongPressGestureRecognizer does not do anything

我想让 UI 按钮仅在按住超过设定的秒数时才响应。所以我这样使用 UILongPressGestureRecognizer:

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)
}

和处理程序

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

如您所见,我使用了 DispatchQueue 并尝试更改按钮的颜色,但均无效。有人可以告诉我为什么吗?

更新 :- 我对实施答案中给出的方法感到困惑,所以我想我会再次提供我的完整代码

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")
    }
   
}

}

您需要将手势添加到按钮而不是视图

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

改为在 viewDidLoad 中添加手势:

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

整个问题是因为,您正在使用 UIBUtton 进行手势识别。你必须使用 Uiview 作为 UILongPressGestureRecognizer .

如果你想要像 UIButton 这样的动画,那么你必须使用手动动画或者你可以准备好从互联网上制作代码。

如果您需要进一步的帮助,可以在评论中提问

所以我在查看其他答案,我发现所有答案都在 Objective-C 中,这就是我发布这个问题的原因...所以因为答案对我不起作用,所以我使用 Swiftify 进行转换来自 This Question 的代码,经过一些修改后,它起作用了。

这是代码片段

    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)
}

然后是 objective-c 函数以确保代码仅在长按发生后触发一次

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

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

(但是,我不明白这个答案和上面给出的一些答案之间的区别...有人可以评论它是如何工作的)

您只需要使用 UIView 创建自定义按钮。向该视图添加长按手势,并在需要的时间触发 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
    }
}