[英]Button as Parameter in Swipe Gesture Action in Swift
I just started working with swipe gestures in Swift. 我刚刚开始在Swift中使用滑动手势。 I am trying to use them with buttons: an action is supposed to be performed when the user swipes across a button. 我正在尝试将它们与按钮一起使用:当用户在按钮上滑动时,应该执行一项操作。
In my viewDidLoad()
of a ViewController-class I got: 在我的ViewController类的viewDidLoad()
,我得到了:
let leftSwipeButton = UISwipeGestureRecognizer(target: self, action: "leftSwipeButtonAction")
leftSwipeButton.direction = .Left
myFirstButton.addGestureRecognizer(leftSwipeButton)
mySecondButton.addGestureRecognizer(leftSwipeButton)
myThirdButton.addGestureRecognizer(leftSwipeButton)
myFirstButton
, mySecondButton
and myThirdButton
are buttons ( UIButton
). myFirstButton
, mySecondButton
和myThirdButton
是按钮( UIButton
)。
And on the same level as viewDidLoad()
I defined the action: 在与viewDidLoad()
相同的级别上,我定义了操作:
func leftSwipeButtonAction() {
// here the .backgroundColor of the button that was swiped is supposed to be set to UIColor.yellowColor()
}
As I want to use leftSwipeButtonAction()
with the same functionality for multiple buttons I do not want to write a function for every single button, but rather pass the UIButton
that was swiped as a parameter to leftSwipeButtonAction()
. 因为我想对多个按钮使用具有相同功能的leftSwipeButtonAction()
,所以我不想为每个按钮都编写一个函数,而是将作为参数滑动的UIButton
传递给leftSwipeButtonAction()
。 Is there a way to do this? 有没有办法做到这一点?
You can send only the UITapGestureRecognizer itself as parameter on selector. 您只能发送UITapGestureRecognizer本身作为选择器上的参数。 You have to put : after selector name 您必须在选择器名称后输入:
let leftSwipeButton = UISwipeGestureRecognizer(target: self, action: "leftSwipeButtonAction:")
func leftSwipeButtonAction(recognizer:UITapGestureRecognizer) {
//You could access to sender view
print(recognizer.view?)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.