UILabel长按识别

时间:2017-03-28 03:25:15

标签: ios swift

编辑:在下面的代码中添加了正确的代码。现在正在工作。

我有一个标签,它在游戏运行时位于游戏的顶部。我希望能够长按标签,然后调出主菜单。现在,当我长按app时出现以下错误。

错误:

  

由于未捕获的异常而终止应用   ' 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

和功能

    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
    }
}

1 个答案:

答案 0 :(得分:3)

试试这个

let longGesture = UILongPressGestureRecognizer(target: self, action: #selector(self.longTap))

我希望这有助于你