gpt4 book ai didi

ios - 无法将 UITapGestureRecognizer 和 UILongPressGestureRecognizer 添加到 UIButton

转载 作者:行者123 更新时间:2023-11-28 11:34:40 25 4
gpt4 key购买 nike

我正在使用以下代码片段来添加手势识别器:

let tapGesture = UITapGestureRecognizer(target: self, action: #selector(attachImage))
let longGesture = UILongPressGestureRecognizer(target: self, action: #selector(longPress))
tapGesture.numberOfTapsRequired = 1
cell.image.addGestureRecognizer(tapGesture)
cell.image.addGestureRecognizer(longGesture)

@objc func longPress(_ btn : UIButton) {
selectedImageIndex = btn.tag
}
@objc func attachImage(_ btn : UIButton) {
selectedImageIndex = btn.tag
}

当我按下按钮时出现以下错误

unrecognized selector sent to instance 0x2802ec000

最佳答案

修改函数如下

@objc func longPress(_ sender : UILongPressGestureRecognizer) {
if let btn = sender.view {
selectedImageIndex = btn.tag
}
}
@objc func attachImage(_ sender : UITapGestureRecognizer) {
if let btn = sender.view {
selectedImageIndex = btn.tag
}
}

并且改变手势初始化如下

let tapGesture = UITapGestureRecognizer(target: self, action: #selector(attachImage(_:)))
let longGesture = UILongPressGestureRecognizer(target: self, action: #selector(longPress(_:)))

关于ios - 无法将 UITapGestureRecognizer 和 UILongPressGestureRecognizer 添加到 UIButton,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55843459/

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