Hold to record (start / stop)
Voice memo style: press starts recording, release stops it. Each boundary gets its own haptic.
import SwiftUI
struct RecordButton: View {
@State private var isRecording = false
var body: some View {
Circle()
.fill(isRecording ? .red : .gray)
.frame(width: 72, height: 72)
.scaleEffect(isRecording ? 1.2 : 1.0)
.animation(.spring(duration: 0.25), value: isRecording)
.onLongPressGesture(minimumDuration: 0.1, perform: {}) { pressing in
isRecording = pressing
// start/stop your AVAudioRecorder here
}
// .start and .stop are semantic activity haptics:
.sensoryFeedback(trigger: isRecording) { _, recording in
recording ? .start : .stop
}
}
} let startHaptic = UIImpactFeedbackGenerator(style: .medium)
let stopHaptic = UIImpactFeedbackGenerator(style: .light)
@objc func handlePress(_ gr: UILongPressGestureRecognizer) {
switch gr.state {
case .began:
startHaptic.impactOccurred()
stopHaptic.prepare()
// recorder.record()
case .ended, .cancelled:
stopHaptic.impactOccurred(intensity: 0.6)
// recorder.stop()
default: break
}
}