我刚开始使用Swift中的滑动手势。我试图将它们与按钮一起使用:当用户在按钮上滑动时,应该执行动作。
在我的viewDidLoad()
ViewController类中,我得到了:
let leftSwipeButton = UISwipeGestureRecognizer(target: self, action: "leftSwipeButtonAction")
leftSwipeButton.direction = .Left
myFirstButton.addGestureRecognizer(leftSwipeButton)
mySecondButton.addGestureRecognizer(leftSwipeButton)
myThirdButton.addGestureRecognizer(leftSwipeButton)
myFirstButton
,mySecondButton
和myThirdButton
是按钮(UIButton
)。
与viewDidLoad()
处于同一级别我定义了操作:
func leftSwipeButtonAction() {
// here the .backgroundColor of the button that was swiped is supposed to be set to UIColor.yellowColor()
}
由于我想对多个按钮使用leftSwipeButtonAction()
具有相同的功能,我不想为每个按钮编写一个函数,而是将作为参数滑动的UIButton
传递给{ {1}}。有没有办法做到这一点?
答案 0 :(得分:1)
您只能将UITapGestureRecognizer本身作为选择器上的参数发送。你必须把:在选择器名称
之后let leftSwipeButton = UISwipeGestureRecognizer(target: self, action: "leftSwipeButtonAction:")
func leftSwipeButtonAction(recognizer:UITapGestureRecognizer) {
//You could access to sender view
print(recognizer.view?)
}