HUD.swift 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. //
  2. // HUD.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 02/01/23.
  6. // Copyright © 2023 Marino Faggiana. All rights reserved.
  7. //
  8. import SwiftUI
  9. struct HUD: View {
  10. @Binding var showHUD: Bool
  11. @State var textLabel: String
  12. @State var image: String
  13. var body: some View {
  14. Button(action: {
  15. withAnimation {
  16. self.showHUD = false
  17. }
  18. }) {
  19. Label(textLabel, systemImage: image)
  20. .foregroundColor(.gray)
  21. .padding(.horizontal, 10)
  22. .padding(14)
  23. .background(
  24. Blur(style: .systemMaterial)
  25. .clipShape(Capsule())
  26. .shadow(color: Color(.black).opacity(0.22), radius: 12, x: 0, y: 5)
  27. )
  28. }.buttonStyle(PlainButtonStyle())
  29. }
  30. }
  31. struct Blur: UIViewRepresentable {
  32. var style: UIBlurEffect.Style
  33. func makeUIView(context: Context) -> UIVisualEffectView {
  34. return UIVisualEffectView(effect: UIBlurEffect(style: style))
  35. }
  36. func updateUIView(_ uiView: UIVisualEffectView, context: Context) {
  37. uiView.effect = UIBlurEffect(style: style)
  38. }
  39. }
  40. struct ContentView: View {
  41. @State private var showHUD = false
  42. @Namespace var hudAnimation
  43. func dismissHUDAfterTime() {
  44. DispatchQueue.main.asyncAfter(deadline: .now() + 2) {
  45. self.showHUD = false
  46. }
  47. }
  48. var body: some View {
  49. GeometryReader { geo in
  50. ZStack(alignment: .top) {
  51. NavigationView {
  52. Button("Save image") {
  53. self.showHUD.toggle()
  54. }
  55. .navigationTitle("Content View")
  56. }
  57. HUD(showHUD: $showHUD, textLabel: "xxx", image: "photo")
  58. .offset(y: showHUD ? (geo.size.height / 2) : -200)
  59. .animation(.easeOut)
  60. }
  61. }
  62. }
  63. }
  64. struct HUD_Previews: PreviewProvider {
  65. static var previews: some View {
  66. ContentView()
  67. }
  68. }