我想在iOS Swift 4. *中使用UILongPressGestureRecognizer方法传递一些参数。

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

@objc func buttonPressedLong(_ sender:UIGestureRecognizer) {

}

最佳答案

我建议您使自定义类继承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
 }

关于ios - 如何在UILongPressGestureRecognizer iOS Swift 4中传递多个参数?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/51552308/

10-11 15:15