我正在尝试为我的聊天实现一个记录按钮,只要您按住该按钮,它就会记录下来。我实现了一个longpressGestureRecognizer,但不幸的是它只记录一秒钟,无论我按下多长时间。
这是代码:
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 事件等。我仍然得到相同的行为,除非我稍微高于记录按钮离开橙色视图。然后录制图像按钮图像也会改变,显示正在录制,如果我松开并进一步向上移动它会停止录制。