2015-01-21 54 views
4

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

预期的效果是让我可以按下按钮以及轻扫以产生不同的结果。到目前为止,我正在实施手势的方式将其应用于我的整个视图,而不仅仅是按钮。

我有一种感觉,它很简单,但它已经逃离了我几天 - 我可能只是在寻找错误的东西。

(我指定 '@IBOutlet VAR swipeButton:UIButton的' 到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") 
} 

} 

回答

5

如果你想swipeGesture添加到按钮,然后像这样做方法:

self.yourButton.addGestureRecognizer(swipeButtonDown) 

,也有在选择了一个错误,你送了它应该是这样的:

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

变化ButtonDownbuttonDown

+0

我将它设置在第13行如下 - 它总是崩溃,所以我肯定做错了什么:d self.swipeButton.addGestureRecognizer(swipeButtonDown) – glasstongue 2015-01-21 05:26:24

+0

显示崩溃日志在题。 – 2015-01-21 05:28:06

+0

我编辑了错误的[这里:var swipeButtonDown:UISwipeGestureRecognizer = UISwipeGestureRecognizer(target:self,action:“ButtonDown”)]并且没有撤消 - 像魅力一样!我喜欢你,但我是一个低调的新手 - 干杯! – glasstongue 2015-01-21 05:41:30