2016-11-15 12 views
0

私はJSQMessagesViewControllerを使用してチャットを実装しています。録音ボタンを追加し、そのボタンを押し下げて録音や録音を停止することで、オーディオメッセージを送信したい。 は現在、私は、このようにそれをやろうとしている:TouchDownイベント録音するUIbuttonがCollectionviewのためにトリガーされていない

record_button = UIButton(type: UIButtonType.custom) 
    record_button.tintColor = UIColor.white 
    record_button.setImage(recordImage, for: .normal) 

    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.addTarget(self, action: #selector(ChatViewController.recording), for: .touchDown) 
        self.record_button.addTarget(self, action: #selector(ChatViewController.recordTapRelease), for: .touchUpInside) 
        self.record_button.addTarget(self, action: #selector(ChatViewController.recordTapRelease), for: .touchDragExit) 
       } else { 
        // failed to record! 
       } 
      } 
     } 
    } catch { 
     // failed to record! 
    } 

残念ながら、それは正しく動作しません。ボタンを押すとタッチが上がるだけです。それだけが、ホールドイベントとレコードを認識します。私が同じ場所に触れていると、それは録音されず、タッチを離すと1秒の音声メッセージが送信されます。

EDIT 私はこの問題は、ScrollViewを持っている私のCollectionviewであることに気づきました。スクロールビューは、ユーザーがスクロールするかどうかをチェックするためにタッチダウンイベントを遅延させます。とにかく、私はタッチダウンイベントをどのように引き起こすのですか?

ChatViewController

答えて

0

さて、あなたは代わりにジェスチャー認識装置を使用することができます。

let gesture = UILongPressGestureRecognizer(target: self, action: #selector(longTouched)) 
button.addGestureRecognizer(gesture) 

func longTouched(gesture: UIGestureRecognizer) { 
    if gesture.state == .began { 
     //triggered after long pressed 
    }else if gesture.state == .ended || gesture.state == .cancelled { 
     //end of the touch 
    } 
} 

タッチの開始を検出する必要がある場合は、ボタンにUITapGestureRecognizerを追加することもできます。 その場合は次のようにします。

tapGesture.numberOfTapsRequired = 1 
+0

これまでに試してみましたが、これで何の成功も見られませんでした。 –

+0

まあ、私は私のプロジェクトでそれを好きでした、それは動作します:)しかし、完全なソースを開くことができません。 – Mikael

+0

私の問題は、scrollViewコントローラです。私の編集をチェックしてください。 –

関連する問題