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

Button as Parameter in Swipe Gesture Action in Swift

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

在我的 viewDidLoad() 中 ViewController-class 我得到了:

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 作为参数传递至 leftSwipeButtonAction()。有办法吗?

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

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

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