【问题标题】:figure out which button was pressed while differ between long-press and tap. swift找出在长按和点击之间不同时按下了哪个按钮。迅速
【发布时间】:2023-03-30 00:02:01
【问题描述】:

所以我有长按和短按按钮的侦听器,但我需要知道按下的是哪个按钮。 是否可以找出在点击和长功能中按下了哪个按钮,或者我需要为每个按钮执行两个功能?

let tapGesture = UITapGestureRecognizer(target: self, action: #selector(tap))
let longGesture = UILongPressGestureRecognizer(target: self, action: #selector(long))
longGesture.minimumPressDuration = 0.5
smallOption.addGestureRecognizer(tapGesture)
mediumOption.addGestureRecognizer(tapGesture)
largeOption.addGestureRecognizer(tapGesture)
smallOption.addGestureRecognizer(longGesture)
mediumOption.addGestureRecognizer(longGesture)
largeOption.addGestureRecognizer(longGesture)

@objc func tap(_ sender: UIGestureRecognizer){
    print("short-press")
}
@objc func long(_ sender: UIGestureRecognizer){
    print("long-press")
}

【问题讨论】:

    标签: ios swift uibutton


    【解决方案1】:

    在这种情况下,主要问题是两种手势都将被添加用于largeOption 按钮!澄清一下,手势只为一个组件添加,在您的情况下,它应该只为最新的一个组件添加(即largeOption):

    smallOption.addGestureRecognizer(tapGesture) <-- skipped
    mediumOption.addGestureRecognizer(tapGesture) <-- skipped
    largeOption.addGestureRecognizer(tapGesture) <-- added
    smallOption.addGestureRecognizer(longGesture) <-- skipped
    mediumOption.addGestureRecognizer(longGesture) <-- skipped
    largeOption.addGestureRecognizer(longGesture) <-- added
    

    从逻辑上讲,这可能是您问题的答案:

    是否可以找出在点击和长功能中按下了哪个按钮,或者我需要为每个按钮执行两个功能?

    您需要为每个按钮添加两个手势,因为特定手势只能添加到一个视图。

    但是,除了@objc func tap(_ sender: UIGestureRecognizer)@objc func long(_ sender: UIGestureRecognizer) 现有的操作方法之外,您不必声明新的操作方法。您可以做的是检查sender 的视图。示例:

    假设我们为每个按钮手动添加了两个手势:

    // gestures:
    let smallOptionTapGesture = UITapGestureRecognizer(target: self, action: #selector(tap))
    let smallOptionLongGesture = UILongPressGestureRecognizer(target: self, action: #selector(long))
    smallOptionLongGesture.minimumPressDuration = 0.5
    
    let mediumOptionTapGesture = UITapGestureRecognizer(target: self, action: #selector(tap))
    let mediumOptionLongGesture = UILongPressGestureRecognizer(target: self, action: #selector(long))
    mediumOptionLongGesture.minimumPressDuration = 0.5
    
    let largeOptionTapGesture = UITapGestureRecognizer(target: self, action: #selector(tap))
    let largeOptionLongGesture = UILongPressGestureRecognizer(target: self, action: #selector(long))
    largeOptionLongGesture.minimumPressDuration = 0.5
    
    // adding them:
    smallOption.addGestureRecognizer(smallOptionTapGesture)
    mediumOption.addGestureRecognizer(mediumOptionTapGesture)
    largeOption.addGestureRecognizer(largeOptionTapGesture)
    
    smallOption.addGestureRecognizer(smallOptionLongGesture)
    mediumOption.addGestureRecognizer(mediumOptionLongGesture)
    largeOption.addGestureRecognizer(largeOptionLongGesture)
    

    因此,你可以做的是:

    @objc func tap(_ sender: UIGestureRecognizer) {
        // an example of how you could check the button
        if sender.view == smallOption {
            print("small short-press")
        } else if sender.view == mediumOption {
            print("medium short-press")
        } else if sender.view == largeOption {
            print("large short-press")
        }
    }
    @objc func long(_ sender: UIGestureRecognizer) {
        // you could apply the same above approach here
    }
    

    另一种选择是分别为每个按钮创建操作方法。

    【讨论】:

      【解决方案2】:

      对于每个按钮的相同手势,您不需要两个函数。更改方法定义以接受UIButton(在您的情况下)而不是UIGestureRecognizer。之后,通过直接验证标签或类型来检查发件人...

      @objc func tap(_ sender: UIButton){
        switch sender {
        case button1: // or by tags of buttons
        case button2:
        ...
        default: break
      }
      

      【讨论】:

        猜你喜欢
        • 2011-11-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-05-31
        相关资源
        最近更新 更多