【问题标题】:Button tap and long press gesture按钮点击和长按手势
【发布时间】:2016-04-05 12:49:09
【问题描述】:

我的手势有点问题。

我正在尝试在同一个按钮上同时使用点击和长按,所以我使用了

@IBAction func xxx (sender: UITapGestureRecognizer)

@IBAction func xxx (sender: UILongPressGestureRecognizer)

但是当我点击时,我的按钮似乎对这两个功能都有反应。可能有什么问题?

func long(longpress: UIGestureRecognizer){
    if(longpress.state == UIGestureRecognizerState.Ended){
    homeScoreBool = !homeScoreBool
    }else if(longpress.state == UIGestureRecognizerState.Began){
        print("began")
    }
}

【问题讨论】:

    标签: ios swift uigesturerecognizer uitapgesturerecognizer


    【解决方案1】:

    很难说你的代码有什么问题,你提供的只有两行,但我建议你改用这种方式:

    改为为您的按钮创建一个出口

    @IBOutlet weak var myBtn: UIButton!
    

    然后在您的 viewDidLoad() 中将手势添加到按钮

    let tapGesture = UITapGestureRecognizer(target: self, action: "normalTap")  
    let longGesture = UILongPressGestureRecognizer(target: self, action: "longTap:")
    tapGesture.numberOfTapsRequired = 1
    myBtn.addGestureRecognizer(tapGesture)
    myBtn.addGestureRecognizer(longGesture)
    

    然后创建处理点击的动作

    func normalTap(){
    
        print("Normal tap")
    }
    
    func longTap(sender : UIGestureRecognizer){
        print("Long tap")
        if sender.state == .Ended {
        print("UIGestureRecognizerStateEnded")
        //Do Whatever You want on End of Gesture
        }
        else if sender.state == .Began {
            print("UIGestureRecognizerStateBegan.")
            //Do Whatever You want on Began of Gesture
        }
    }
    

    Swift 3.0 版本:

    let tapGesture = UITapGestureRecognizer(target: self, action: #selector(ViewController.normalTap))
    let longGesture = UILongPressGestureRecognizer(target: self, action: Selector(("longTap:")))
    tapGesture.numberOfTapsRequired = 1
    myBtn.addGestureRecognizer(tapGesture)
    myBtn.addGestureRecognizer(longGesture)
    
    func normalTap(){
    
        print("Normal tap")
    }
    
    func longTap(sender : UIGestureRecognizer){
        print("Long tap")
        if sender.state == .ended {
            print("UIGestureRecognizerStateEnded")
            //Do Whatever You want on End of Gesture
        }
        else if sender.state == .began {
            print("UIGestureRecognizerStateBegan.")
            //Do Whatever You want on Began of Gesture
        }
    }
    

    更新了 Swift 5.x 的语法:

    let tapGesture = UITapGestureRecognizer(target: self, action: #selector(normalTap))
    button.addGestureRecognizer(tapGesture)
    
    let longGesture = UILongPressGestureRecognizer(target: self, action: #selector(longTap))
    button.addGestureRecognizer(longGesture)
    
    @objc func normalTap(_ sender: UIGestureRecognizer){
        print("Normal tap")
    }
    
    @objc func longTap(_ sender: UIGestureRecognizer){
        print("Long tap")
        if sender.state == .ended {
            print("UIGestureRecognizerStateEnded")
            //Do Whatever You want on End of Gesture
        }
        else if sender.state == .began {
            print("UIGestureRecognizerStateBegan.")
            //Do Whatever You want on Began of Gesture
        }
    }
    

    【讨论】:

    • 谢谢你它工作得很好,但是当我尝试两次时使用长按两次,你知道为什么吗?谢谢楼主
    • 可能是在捕捉双击?
    • @AlvinWan,很抱歉回复晚了,但这是因为UILongPressGestureRecognizer 有两个状态,开始和结束。我已经用一个例子更新了代码。请注意,我在 longTap 中添加了一个参数,并在 longGesture UILongPressGestureRecognizer 中添加了一个“:”。
    • 如果我有更多按钮...比如 myBtn1、myBtn2 和 myBtn3 怎么办?
    • @arakweker,是的,那么您可以为此使用标签。您可以通过sender.view?.tag 访问该标签。另请注意,每个按钮都必须有自己的手势声明,但您仍然可以调用相同的函数。例如:let tapForFirstButton = UITapGestureRecognizer(target: self, action: #selector(normalTap(_:)))let tapForSecondButton = UITapGestureRecognizer(target: self, action: #selector(normalTap(_:)))
    猜你喜欢
    • 2013-07-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-13
    • 2017-08-12
    相关资源
    最近更新 更多