繁体   English   中英

如何在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) {

}

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

暂无
暂无

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

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