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

How to pass multiple parameters in UILongPressGestureRecognizer iOS Swift 4?

我想在 iOS Swift 4.* .

中使用 UILongPressGestureRecognizer 方法传递一些参数
let buttonLongGesture = UILongPressGestureRecognizer(target: self, action: #selector(buttonPressedLong(_:)))
button.addGestureRecognizer(buttonLongGesture)

@objc func buttonPressedLong(_ sender:UIGestureRecognizer) {

}

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

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
 }