UILabel长按识别

idl*_*and 5 ios swift

编辑:将正确的代码添加到下面的代码中。工作中。

我的游戏运行时有一个位于顶部的标签。我希望能够长按标签并调出主菜单。现在,当我长按时,应用程序崩溃并出现以下错误。

错误:

由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“-[blocks1.ViewController longTap:]:无法识别的选择器发送到实例 0x101800000”*** 首先抛出调用堆栈:

我所拥有的基础知识:

var gameTopTitle = UILabel()

//this is all after my gameTopTitle is added to the screen
let longGesture = UILongPressGestureRecognizer(target: self, action: #selector(self.longTap))
gameTopTitle.addGestureRecognizer(longGesture)
gameTopTitle.isUserInteractionEnabled = true
Run Code Online (Sandbox Code Playgroud)

和函数

    func longTap(sender : UIGestureRecognizer){
    print("Long tap")
    if sender.state == .ended {
        print("UIGestureRecognizerStateEnded")
        //Do Whatever You want on End of Gesture
    }
    else if sender.state == .began {
        print("UIGestureRecognizerStateBegan.")
        //Do Whatever You want on Began of Gesture
    }
}
Run Code Online (Sandbox Code Playgroud)

Rei*_*ian 5

尝试用这个

let longGesture = UILongPressGestureRecognizer(target: self, action: #selector(self.longTap))
Run Code Online (Sandbox Code Playgroud)

我希望这可以帮助你