1

我在这里搜索了有关堆栈溢出的所有问题,但我似乎无法弄清楚,因为我似乎做的一切都是正确的,但我仍然收到这个错误。我正在尝试为我的聊天实现一个录制按钮,只要按下按钮就会录制。我总是收到以下错误: [HenrysApp.ChatViewController longPress:]: unrecognized selector sent to instance 0x7f952602dc00

这是代码:

let longPressGestureRecognizer = UILongPressGestureRecognizer(target: self, action: "longPress:")
    longPressGestureRecognizer.minimumPressDuration = 1


    self.recordingSession = AVAudioSession.sharedInstance()

    do {
        try self.recordingSession.setCategory(AVAudioSessionCategoryPlayAndRecord)
        try self.recordingSession.setActive(true)
        self.recordingSession.requestRecordPermission() { [unowned self] allowed in
            DispatchQueue.main.async {
                if allowed {
                    self.record_button.addGestureRecognizer(longPressGestureRecognizer)
                } else {
                    // failed to record!
                }
            }
        }
    } catch {
        // failed to record!
    }


 // Gesture Recognizer for the Record Button, so as long as it is pressed, record!
func longPress(longPressGestureRecognizer: UILongPressGestureRecognizer){
    if longPressGestureRecognizer.state == .began {
        print("long press began")
        let recordingTapImage = UIImage(named: "ic_mic_none_white")
        record_button.setImage(recordingTapImage, for: .normal)
        self.recording()
    }
    if longPressGestureRecognizer.state == .ended {
        print("long press ended")
        let recordImage = UIImage(named: "ic_mic_white")
        record_button.setImage(recordImage, for: .normal)
        self.recordTapRelease()
    }
}
4

3 回答 3

0
  • 这可能是因为插座可能未正确连接。单击视图控制器或情节提要中的任何 UI 元素,然后打开右侧的连接检查器。在那里您可能会发现任何 UI 元素是否正确连接。
于 2016-11-14T13:57:36.960 回答
0

先尝试 t 替换"longPress:"#selector(longPress(_:))( first line ),这是新的命名约定

于 2016-11-14T13:54:16.817 回答
0

你需要改变

let longPressGestureRecognizer = UILongPressGestureRecognizer(target: self, action: "longPress:")

有了这个

let longPressGestureRecognizer = UILongPressGestureRecognizer(target: self, action: #selector(longPress(_:)))
于 2016-11-14T13:55:05.480 回答