1

ボタンを押している限り記録するチャットの記録ボタンを実装しようとしています。longpressGestureRecognizer を実装しましたが、残念ながらどれだけ押しても 1 秒間しか記録されません。

コードは次のとおりです。

let longPressGestureRecognizer = UILongPressGestureRecognizer(target: self, action: #selector(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 == .ended {
        print("long press ended")
        let recordImage = UIImage(named: "ic_mic_white")
        record_button.setImage(recordImage, for: .normal)
        self.recordTapRelease()
    }
    if longPressGestureRecognizer.state == .began {
        let recordingTapImage = UIImage(named: "ic_mic_none_white")
        record_button.setImage(recordingTapImage, for: .normal)
        self.recording()

    }
}

編集 .touchdown .touchupinside イベントなどを実装しました。オレンジ色のビューを残して記録ボタンの上にわずかに上がらない限り、同じ動作が得られます。次に、録画画像ボタンの画像も変更され、録画を示します。リリースしてさらに上に移動すると、録画が停止します。

録音ボタン機能

4

1 に答える 1

1

UILongPressGestureRecognizerこれを達成するために作成する必要さえありません。UIButton にtouchDowntouchUpInsideおよびイベントを実装することでそれを行うことができます。touchDragExit

一見、 での作業よりも複雑に見えるかもしれませんが、UILongPressGestureRecognizerより論理的で読みやすいと思います。

この回答の手順に従ってください。うまくいけば、録音ボタンの望ましい動作が得られます。を使用することを主張する場合は、別の答えもありますUILongPressGestureRecognizer

これが役に立ったことを願っています。

于 2016-11-14T15:31:05.720 に答える