Created
October 10, 2022 20:23
-
-
Save EthanLipnik/cc5007e7c347eaa2b889e6af7f74348f to your computer and use it in GitHub Desktop.
Audio Recording Button UI in SwiftUI
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import SwiftUI | |
struct AudioRecorderView: View { | |
@Environment(\.dismiss) var dismiss | |
@StateObject var audioService = AudioRecordService() | |
@Namespace private var nspace | |
var body: some View { | |
VStack { | |
Spacer() | |
Group { | |
if let recordedFile = audioService.recordedFile { | |
HStack(spacing: 20) { | |
Button { | |
audioService.recordedFile = nil | |
} label: { | |
Text("Redo") | |
.rounded(.headline) | |
.padding(.horizontal) | |
} | |
.controlSize(.large) | |
.buttonBorderShape(.roundedRectangle(radius: 20)) | |
.buttonStyle(.bordered) | |
Button { | |
} label: { | |
Text("Post") | |
.rounded(.headline) | |
.frame(maxWidth: .infinity) | |
} | |
.controlSize(.large) | |
.buttonBorderShape(.roundedRectangle(radius: 20)) | |
.buttonStyle(.borderedProminent) | |
.matchedGeometryEffect(id: "primaryButton", in: nspace) | |
} | |
} else { | |
Button { | |
audioService.toggleRecording() | |
} label: { | |
ZStack { | |
if !audioService.isRecording { | |
Circle() | |
.stroke(Color.white, lineWidth: 2) | |
.transition(.blur.animation(.easeInOut)) | |
} | |
RoundedRectangle(cornerRadius: !audioService.isRecording ? 64 / 2 : 15, style: .continuous) | |
.fill(Color.red) | |
.padding(!audioService.isRecording ? 4 : 0) | |
.animation(.interpolatingSpring(mass: 1, stiffness: 180, damping: 12, initialVelocity: 0), value: audioService.isRecording) | |
}.compositingGroup() | |
} | |
.matchedGeometryEffect(id: "primaryButton", in: nspace) | |
.frame(width: 64, height: 64) | |
} | |
} | |
.transition(.blur.animation(.easeInOut)) | |
} | |
.animation(.spring(), value: audioService.recordedFile) | |
.padding() | |
.blur(radius: audioService.isAuthorized ? 0 : 20) | |
.animation(.spring(), value: audioService.isAuthorized) | |
.preferredColorScheme(.dark) | |
.onAppear { | |
audioService.requestPermission() | |
} | |
.onDisappear { | |
audioService.destroy() | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is the blur transition used