UILongPressGestureRecognizer不执行任何操作

时间:2020-06-24 10:56:01

标签: ios swift uibutton

我想让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")
    }
   
}

}

5 个答案:

答案 0 :(得分:1)

您只需要使用UIView创建自定义按钮。向该视图添加长按手势,然后在需要的时间触发代表/关闭。

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

答案 1 :(得分:0)

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

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

答案 2 :(得分:0)

将手势添加到您的viewDidLoad

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

答案 3 :(得分:0)

全是因为您使用UIBUtton进行手势识别。您必须为Uiview使用UILongPressGestureRecognizer

如果您想要UIButton之类的动画,则必须使用手动动画,或者可以准备从Internet编写代码。

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

答案 4 :(得分:0)

所以我在查看其他答案,发现所有答案都在Objective-C中,这就是为什么我发布了这个问题...所以,因为答案对我不起作用,所以我使用了Swiftify将代码从{ {3}},并进行了一些修改后就可以了。

这是代码段

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

(但是,我无法理解此答案与上面给出的某些答案之间的区别……有人可以对其进行评论)