Ios 快速双点击识别器响应单点击识别器
我的应用程序有一个奇怪的问题;当我向标签添加单点击识别器时,它工作正常:Ios 快速双点击识别器响应单点击识别器,ios,swift,Ios,Swift,我的应用程序有一个奇怪的问题;当我向标签添加单点击识别器时,它工作正常: private func addDoubleTabGestureRecognizerToWordLabel(){ wordLabel.isUserInteractionEnabled = true let gestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(labelClicked(gesture:)
private func addDoubleTabGestureRecognizerToWordLabel(){
wordLabel.isUserInteractionEnabled = true
let gestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(labelClicked(gesture:)))
wordLabel.addGestureRecognizer(gestureRecognizer)
}
或
但当我把水龙头号码改成其他类似的号码时
private func addDoubleTabGestureRecognizerToWordLabel(){
wordLabel.isUserInteractionEnabled = true
let gestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(labelClicked(gesture:)))
gestureRecognizer.numberOfTapsRequired = 2
wordLabel.addGestureRecognizer(gestureRecognizer)
}
点击标签时出现以下错误:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[PolyLibrum.WordLabel showInfoAroundWordLabelWhenTabbed:]: unrecognized selector sent to instance 0x7fdea951b2d0'
更奇怪的是,当我点击标签一次而不是两次时,就会触发错误
以下是选择器功能:
@objc func labelClicked(gesture: UITapGestureRecognizer) {
print("UILabel clicked")
//executed when the label is double tapped
//worked when I set the gestureRecognizer.numberOfTapsRequired = 1
let bubbleFrame = wordLabel.frame;
let translationBubble = TranslationBubble(frame: bubbleFrame)
wordFrame.addSubview(translationBubble as! UIImageView)
}
为什么识别器在单次点击而不是双击时被触发?选择器功能与触发它所需的点击次数有什么关系
感谢您的帮助。这可能是由于管理不善,无法为该识别器添加点击手势。您可能不需要将手势发送到选择器功能 改变这个
让gestureRecognizer=UITapGestureRecognizer(目标:自我,操作:#选择器(labelClicked(手势:))
到
让gestureRecognizer=UITapGestureRecognizer(目标:自我,操作:#选择器(labelClicked))
在中添加DoubleTabgestureRecognitizerToOrdLabel()
并删除labelClicked()
中的接收参数
希望这有帮助 事实证明,正如Sweeper在评论中所说的那样,我忘了删除我之前正在试验的interface builder手势识别器。当我移除元素时,一切都正常。再次感谢您的回答。在项目中搜索“showInfoAroundWordLabelWhenTabbed”一词,尤其是在故事板/XIB文件中。当你不知道标签不存在时,可能有什么东西触发了选择器。非常感谢你的回答。不幸的是,它没有解决问题,但它确实清理了我的代码很多。“姿态”的论点确实是多余的。非常感谢。
@objc func labelClicked(gesture: UITapGestureRecognizer) {
print("UILabel clicked")
//executed when the label is double tapped
//worked when I set the gestureRecognizer.numberOfTapsRequired = 1
let bubbleFrame = wordLabel.frame;
let translationBubble = TranslationBubble(frame: bubbleFrame)
wordFrame.addSubview(translationBubble as! UIImageView)
}