Swift -如何使用UILongPressGestureRecognizer获取按钮数组的发件人标签?

cnh2zyt3  于 2023-02-03  发布在  Swift
关注(0)|答案(5)|浏览(132)

我将故事板中的按钮放入引用插座集合中。我对所有这些按钮使用UITapGestureRecognizer和UILongPressGestureRecognizer。但我如何准确地打印出哪个按钮被点击了呢?我尝试使用了Bellow,但不起作用。我收到一个错误消息,称"'UILongPressGestureRecognizer'类型的值没有成员'tag'"。我正在尝试为扫雷游戏构建按钮网格。谢谢你的帮助。
类视图控制器:UIView控制器{@IBOutlet变量测试按钮:[UIButton] !//此数组中有100个按钮

override func viewDidLoad() {
    super.viewDidLoad()

    let testButtonPressed = UILongPressGestureRecognizer(target: self, action: #selector(testPressed))
    testButtonPressed.minimumPressDuration = 0.5
    // These indexes are just to test how to recognize which button gets pressed
    testButtons[0].addGestureRecognizer(testButtonPressed)
    testButtons[1].addGestureRecognizer(testButtonPressed)

}

@objc func testPressed(_ sender: UILongPressGestureRecognizer) {
    print("Test button was pressed")

    print(sender.tag) // THIS DOESN'T WORK, BUT CONCEPTUALLY THIS IS WHAT I WANT TO DO
}
idv4meu8

idv4meu81#

发生此错误的原因是UILongPressGestureRecognizer对象没有tag属性
您可以通过以下方式访问发件人按钮:

@objc func testPressed(_ sender: UILongPressGestureRecognizer) {
    guard let button = sender.view as? UIButton else { return }
    print(button.tag)
}

我认为处理按钮操作的最佳解决方案是添加@IBAction(您可以像添加@IBOutlet一样添加它,只需做一个小的更改-设置Action连接类型)
然后在@IBAction块中,您可以访问所有按钮属性(如tag等)

lp0sw83n

lp0sw83n2#

与其使用手势,我认为使用@IBAction并将按钮与它连接起来会更好

brccelvz

brccelvz3#

UILongPressGestureRecognizer是UIGestureRecognizer的子类,每个按钮或视图只能使用一次。因为UILongPressGestureRecognizer只有一个视图属性。在您的代码中,调用testPressed操作的始终是testButtons[1]。因此,您必须首先修改viewLoad代码,如下所示:-DidLoad

for button in testButtons {
        let testButtonPressed = UILongPressGestureRecognizer(target: self, action: #selector(testPressed))
        testButtonPressed.minimumPressDuration = 0.5
        button.addGestureRecognizer(testButtonPressed)
        button.addGestureRecognizer(testButtonPressed)
    }

然后,您可以从testPressed访问该按钮,如下所示(我希望您已经在故事板中设置了标记):

@objc func testPressed(_ sender: UILongPressGestureRecognizer) {

    if sender.state == .began {
        if let button = sender.view as? UIButton {
            print(button.tag)
        }
    }
}
3pvhb19x

3pvhb19x4#

您需要在按下之前设置标签!在viewDidLoad()方法上,您必须添加如下内容:

testButtons.enumerated().forEach {
     let testButtonPressed = UILongPressGestureRecognizer(target: self, action: #selector(testPressed))
     testButtonPressed.minimumPressDuration = 0.5
     $0.element.addGestureRecognizer(testButtonPressed)
     $0.element.tag = $0.offset
}

而当长按正在接收时,你需要从视图中获得标签,而不是从发件人那里!

print(sender.view?.tag)
9o685dep

9o685dep5#

由于手势识别器只能与单个视图相关联,并且不直接支持使用标识标记来匹配按钮,因此在为键盘创建按钮阵列时,使用单个手势响应函数,我发现使用手势识别器的“name”属性来标识关联按钮更容易。

var allNames: [String] = []

// MARK: Long Press Gesture
func addButtonGestureRecognizer(button: UIButton, name: String) {
    let longPrssRcngr = UILongPressGestureRecognizer.init(target: self, action: #selector(longPressOfButton(gestureRecognizer:)))
    longPrssRcngr.minimumPressDuration    = 0.5
    longPrssRcngr.numberOfTouchesRequired = 1
    longPrssRcngr.allowableMovement       = 10.0
    longPrssRcngr.name = name
    allNames.append(name)
    button.addGestureRecognizer(longPrssRcngr)
}

// MARK: Long Key Press
@objc func longPressOfButton(gestureRecognizer: UILongPressGestureRecognizer) {
    print("\nLong Press Button => \(String(describing: gestureRecognizer.name)) : State = \(gestureRecognizer.state)\n")
    if gestureRecognizer.state == .began || gestureRecognizer.state == .changed {
        if let keyName = gestureRecognizer.name {
            if allNames.contains(keyName) {
                insertKeyText(key: keyName)
            } else {
                print("No action available for key")
            }
        }
    }
}

要实现,在创建按钮后调用addButtonGestureRecognizer函数,并为按钮提供名称(我使用了按钮文本),例如:

addButtonGestureRecognizer(button: keyButton, name: buttonText)

按钮名称存储在“allNames”字符串数组中,以便以后在“longPressOfButton”中进行匹配。
当按钮名称在“longPressOfButton”响应函数中匹配时,它将其传递给“addKeyFunction”进行处理。

相关问题