【问题标题】:UILongPressGestureRecognizer does not do anythingUILongPressGestureRecognizer 不做任何事情
【发布时间】:2020-10-14 14:49:07
【问题描述】:

我希望 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")
    }
   
}

}

【问题讨论】:

  • 按一下按钮,然后尝试长按,这样可以吗?
  • 查看答案是否对您有用?
  • @Frankenstein 不,它没有。感谢您的尝试!

标签: ios swift uibutton


【解决方案1】:

你需要给按钮而不是视图添加手势

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

【讨论】:

  • 感谢您的快速回复!不幸的是,这对我不起作用......也许这是我最终实施它的错误?我是 swift 新手,没有使用过 didSet 和 UILongPressGestureRecognizer,所以我可能做错了
【解决方案2】:

改为在您的viewDidLoad 中添加手势:

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

【讨论】:

    【解决方案3】:

    整个问题是因为您使用UIBUtton 进行手势识别。 UILongPressGestureRecognizer 必须使用 Uiview

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

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

    【讨论】:

    • 所以如果我使用`UILongPressGestureRecognizer`,用户可以点击屏幕上的任意位置,即使用户没有点击按钮或...?
    【解决方案4】:

    所以我在查看其他答案,发现所有答案都在 Objective-C 中,这就是我发布这个问题的原因......所以因为答案对我不起作用,我使用 Swiftify 将代码从 @ 987654321@,经过一些修改,成功了。

    这里是代码sn-p

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

    (但是,我无法理解这个答案与上面给出的一些答案之间的区别......有人可以评论它是如何工作的)

    【讨论】:

      【解决方案5】:

      您只需要使用 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
          }
      }
      

      【讨论】:

      • 谢谢,但只有两个问题,如果我修改#selector 以读取#selector(longPressed_:) 并且它会被触发两次,而它应该只被触发一次时,它才会被触发。
      • 我想通了,您需要将if recognizer?.state == .began { 放入longPressed。非常感谢!
      • @Kingteena 你想在按钮或视图上添加这个手势吗?
      • 我正在尝试在一个按钮中实现。你能告诉我为什么需要下划线吗?我对 swift 有点陌生
      • @Kingteena 请在 swift 中检查命名和未命名的参数,基本上使用下划线可以忽略参数名称而只传递值。更新了我的答案,让未来的 SO 用户受益!!!
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-15
      • 2017-12-31
      相关资源
      最近更新 更多