在Swift中滑动手势动作中的按钮作为参数

时间:2015-12-17 16:52:57

标签: ios swift uibutton uiswipegesturerecognizer swipe-gesture

我刚开始使用Swift中的滑动手势。我试图将它们与按钮一起使用:当用户在按钮上滑动时,应该执行动作。

在我的viewDidLoad() ViewController类中,我得到了:

let leftSwipeButton = UISwipeGestureRecognizer(target: self, action: "leftSwipeButtonAction")
leftSwipeButton.direction = .Left

myFirstButton.addGestureRecognizer(leftSwipeButton)
mySecondButton.addGestureRecognizer(leftSwipeButton)
myThirdButton.addGestureRecognizer(leftSwipeButton)

myFirstButtonmySecondButtonmyThirdButton是按钮(UIButton)。

viewDidLoad()处于同一级别我定义了操作:

    func leftSwipeButtonAction() {
    // here the .backgroundColor of the button that was swiped is supposed to be set to UIColor.yellowColor()
}

由于我想对多个按钮使用leftSwipeButtonAction()具有相同的功能,我不想为每个按钮编写一个函数,而是将作为参数滑动的UIButton传递给{ {1}}。有没有办法做到这一点?

1 个答案:

答案 0 :(得分:1)

您只能将UITapGestureRecognizer本身作为选择器上的参数发送。你必须把:在选择器名称

之后
let leftSwipeButton = UISwipeGestureRecognizer(target: self, action: "leftSwipeButtonAction:")

func leftSwipeButtonAction(recognizer:UITapGestureRecognizer) {
    //You could access to sender view
    print(recognizer.view?)
}