ボタンを押している限り記録するチャットの記録ボタンを実装しようとしています。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 イベントなどを実装しました。オレンジ色のビューを残して記録ボタンの上にわずかに上がらない限り、同じ動作が得られます。次に、録画画像ボタンの画像も変更され、録画を示します。リリースしてさらに上に移動すると、録画が停止します。