在swift中为按钮添加手势 [英] Adding gestures to a button in swift

查看:99
本文介绍了在swift中为按钮添加手势的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试向我视图中的现有按钮添加手势(滑动)功能,但我无法弄清楚如何将滑动附加到按钮的区域。

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.

(我将'@IBOutlet var swipeButton:UIButton!'分配给我的按钮BTW)

(I'm assigning '@IBOutlet var swipeButton: UIButton!' to my button BTW)

以下代码:

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

}


推荐答案

如果你想将swipeGesture添加到Button中,那就这样做:

If you want to add swipeGesture into Button then do it like this way:

self.yourButton.addGestureRecognizer(swipeButtonDown)

也有雾你发送的选择器中的ake应该是这样的:

and also there is a mistake in selector you sent it should be like:

var swipeButtonDown: UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: "buttonDown")

ButtonDown 改为 buttonDown

这篇关于在swift中为按钮添加手势的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆