如何在UILongPressGestureRecognitor iOS Swift 4中传递多个参数?

如何在UILongPressGestureRecognitor iOS Swift 4中传递多个参数?,ios,swift,uigesturerecognizer,gesture,target,Ios,Swift,Uigesturerecognizer,Gesture,Target,我想用iOS Swift 4.*中的UILongPressGestureRecognitor方法传递一些参数 let buttonLongGesture = UILongPressGestureRecognizer(target: self, action: #selector(buttonPressedLong(_:))) button.addGestureRecognizer(buttonLongGesture) @objc func buttonPressedLong(_ sender:

我想用iOS Swift 4.*中的UILongPressGestureRecognitor方法传递一些参数

let buttonLongGesture = UILongPressGestureRecognizer(target: self, action: #selector(buttonPressedLong(_:)))
button.addGestureRecognizer(buttonLongGesture)

@objc func buttonPressedLong(_ sender:UIGestureRecognizer) {

}

我建议您创建一个自定义类继承UI LongPressGestureRecognitor,然后您可以在其中添加任何参数作为变量。最后,您可以使用它在手势发生时发送参数。这里有一个例子

class CustomLongPressGesture: UILongPressGestureRecognizer {
    var firstParam: String!
    var secondParam: String!
}
然后您可以这样实现它:

func setUp() {
    let buttonLongGesture = CustomLongPressGesture(target: self, action: #selector(buttonPressedLong(_:)))
    buttonLongGesture.firstParam = "Test"
    buttonLongGesture.secondParam = "Second Test"
    button.addGestureRecognizer(buttonLongGesture)
}

 @objc func buttonPressedLong(_ sender: CustomLongPressGesture) {
    print(sender.firstParam, sender.secondParam) // Access it here
 }

@LalKrishna现在检查一下。以这种方式传递参数既奇怪又混乱。谢谢。这是一个有趣的答案。这是我的荣幸:D@reza_khalafi