Helper+ButtonStyles.swift 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. import SwiftUI
  2. struct WatchOSButtonStyle: ButtonStyle {
  3. let deviceType: WatchSize
  4. var foregroundColor: Color = .white
  5. var fontSize: Font = .title2
  6. @Environment(\.isEnabled) private var isEnabled: Bool
  7. private var fontWeight: Font.Weight {
  8. switch deviceType {
  9. case .watch40mm:
  10. return .medium
  11. case .watch41mm:
  12. return .medium
  13. case .watch42mm:
  14. return .medium
  15. case .watch44mm:
  16. return .semibold
  17. case .watch45mm:
  18. return .semibold
  19. case .watch49mm:
  20. return .bold
  21. case .unknown:
  22. return .semibold
  23. }
  24. }
  25. private var buttonPadding: CGFloat {
  26. switch deviceType {
  27. case .watch40mm:
  28. return 6
  29. case .watch41mm:
  30. return 6
  31. case .watch42mm:
  32. return 6
  33. case .watch44mm:
  34. return 8
  35. case .watch45mm:
  36. return 8
  37. case .watch49mm:
  38. return 8
  39. case .unknown:
  40. return 8
  41. }
  42. }
  43. func makeBody(configuration: Configuration) -> some View {
  44. var buttonBackground: Color {
  45. if isEnabled {
  46. return Color.tabBar.opacity(configuration.isPressed ? 0.8 : 1.0)
  47. } else {
  48. return Color.tabBar.opacity(0.4)
  49. }
  50. }
  51. configuration.label
  52. .font(fontSize)
  53. .fontWeight(fontWeight)
  54. .padding(buttonPadding)
  55. .background(buttonBackground)
  56. .clipShape(Circle())
  57. .animation(.easeInOut(duration: 0.1), value: configuration.isPressed)
  58. }
  59. }
  60. struct PressableIconButtonStyle: ButtonStyle {
  61. func makeBody(configuration: Configuration) -> some View {
  62. configuration.label
  63. .background(Color.clear)
  64. .opacity(configuration.isPressed ? 0.3 : 1.0) // Change opacity when pressed
  65. .animation(.easeInOut(duration: 0.25), value: configuration.isPressed) // Smooth transition
  66. }
  67. }