ios 按钮点击和长按手势

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34548263/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 08:29:22  来源:igfitidea点击:

Button tap and long press gesture

iosswiftuigesturerecognizeruitapgesturerecognizer

提问by Alvin Wan

I'm having a little trouble with the gestures.

我的手势有点问题。

I'm trying to use both tap and long press on the same button, so I've used

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

@IBAction func xxx (sender: UITapGestureRecognizer)

and

@IBAction func xxx (sender: UILongPressGestureRecognizer)

but my button seems to react to both functions when I tap. What might be wrong?

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

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

回答by Rashwan L

Hard to say what′s not working with your code, with the only two rows that you have provided, but I would recommend you to do it in this way instead:

很难说什么对您的代码不起作用,您只提供了两行,但我建议您改用这种方式:

Create an outlet to your button instead

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

@IBOutlet weak var myBtn: UIButton!

And in your viewDidLoad()add the gestures to the buttons

并在您viewDidLoad()的按钮上添加手势

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

And then create the actions to handle the taps

然后创建操作来处理水龙头

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 version:

斯威夫特 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
    }
}

Updated syntax for Swift 5.x:

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