xcode 快速向按钮添加手势
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28059381/
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
Adding gestures to a button in swift
提问by glasstongue
I'm trying to add gesture(swipe) functionality to an existing button in my view but I can't figure out how to attach the swipe to the button's area.
我正在尝试向视图中的现有按钮添加手势(滑动)功能,但我无法弄清楚如何将滑动附加到按钮区域。
The desired effect is to have a button I can press as well as swipe to produce different outcomes. So far the way I'm implementing gestures applies it to my entire view not just the button.
想要的效果是有一个按钮,我可以按下和滑动来产生不同的结果。到目前为止,我实现手势的方式将它应用于我的整个视图,而不仅仅是按钮。
I have a feeling it's pretty simple but it's been escaping me for a couple of days - I might just be searching for the wrong thing.
我有一种感觉,这很简单,但它已经让我逃避了几天 - 我可能只是在寻找错误的东西。
(I'm assigning '@IBOutlet var swipeButton: UIButton!' to my button BTW)
(顺便说一句,我正在将“@IBOutlet var swipeButton: UIButton!”分配给我的按钮)
Code below:
代码如下:
class ViewController: UIInputViewController {
@IBOutlet var swipeButton: UIButton!
let swipeRec = UISwipeGestureRecognizer()
override func viewDidLoad() {
super.viewDidLoad()
self.loadInterface()
var swipeButtonDown: UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: "ButtonDown")
swipeButtonDown.direction = UISwipeGestureRecognizerDirection.Down
self.view.addGestureRecognizer(swipeButtonDown)
}
@IBAction func buttonPressed(sender: UIButton) {
var proxy = textDocumentProxy as UITextDocumentProxy
proxy.insertText("button")
}
func buttonDown(){
var proxy = textDocumentProxy as UITextDocumentProxy
proxy.insertText("swipe")
}
}
回答by Dharmesh Kheni
If you want to add swipeGesture into Button then do it like this way:
如果要将 swipeGesture 添加到 Button 中,请按以下方式操作:
self.yourButton.addGestureRecognizer(swipeButtonDown)
and also there is a mistake in selector you sent it should be like:
并且您发送的选择器也有错误,它应该是这样的:
var swipeButtonDown: UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: "buttonDown")
change ButtonDown
to buttonDown
更改ButtonDown
为buttonDown