繁体   English   中英

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

[英]Button as Parameter in Swipe Gesture Action in Swift

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

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

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?)
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM