gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-11-01 06:27:50 25 4
gpt4 key购买 nike

我想在 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com