Floating Action Button
Material Design floating action button with animations

Preview

Code
1import SwiftUI23struct FloatingActionButtonView: View {4 @State private var isExpanded = false5 @State private var selectedAction: String?6 7 var body: some View {8 ZStack {9 VStack {10 Spacer()11 12 if isExpanded {13 VStack(spacing: 16) {14 ActionButton(icon: "camera.fill", title: "Camera", color: .blue) {15 selectedAction = "Camera"16 collapseMenu()17 }18 19 ActionButton(icon: "photo.fill", title: "Gallery", color: .green) {20 selectedAction = "Gallery"21 collapseMenu()22 }23 24 ActionButton(icon: "doc.fill", title: "Document", color: .orange) {25 selectedAction = "Document"26 collapseMenu()27 }28 }29 .transition(.scale.combined(with: .opacity))30 }31 32 Button(action: {33 withAnimation(.spring()) {34 isExpanded.toggle()35 }36 }) {37 Image(systemName: isExpanded ? "xmark" : "plus")38 .font(.title2)39 .fontWeight(.semibold)40 .foregroundColor(.white)41 .frame(width: 56, height: 56)42 .background(Color.blue)43 .clipShape(Circle())44 .shadow(radius: 8)45 .rotationEffect(.degrees(isExpanded ? 45 : 0))46 }47 }48 .padding()49 }50 .onChange(of: selectedAction) { action in51 if let action = action {52 print("Selected: \(action)")53 }54 }55 }56 57 func collapseMenu() {58 withAnimation(.spring()) {59 isExpanded = false60 }61 }62}6364struct ActionButton: View {65 let icon: String66 let title: String67 let color: Color68 let action: () -> Void69 70 var body: some View {71 Button(action: action) {72 HStack {73 Image(systemName: icon)74 .foregroundColor(.white)75 .frame(width: 20, height: 20)76 77 Text(title)78 .foregroundColor(.white)79 .font(.body)80 81 Spacer()82 }83 .padding(.horizontal, 16)84 .padding(.vertical, 12)85 .background(color)86 .cornerRadius(25)87 .shadow(radius: 4)88 }89 }90}9192struct FloatingActionButtonView_Previews: PreviewProvider {93 static var previews: some View {94 FloatingActionButtonView()95 }96}