Guest User

SwiftUI RecordButton

a guest
Jan 7th, 2021
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 2.85 KB | None | 0 0
  1. import SwiftUI
  2.  
  3. struct RecordButtons: View {
  4.    
  5.     var body: some View {
  6.         ZStack {
  7.             Color.gray
  8.             HStack {
  9.                 NaiveButton()
  10.                 RecordButton()
  11.                 PhotoButton()
  12.             }
  13.         }
  14.     }
  15. }
  16.  
  17. struct NaiveButton: View {
  18.     @State var isRecording = false
  19.    
  20.     var body: some View {
  21.         Circle()
  22.             .strokeBorder(Color.white, lineWidth: 4.0)
  23.             .frame(width: 70, height: 70)
  24.             .overlay(
  25.                 RoundedRectangle(cornerRadius: isRecording ? 10: 50)
  26.                     .fill(Color.red)
  27.                     .scaleEffect(isRecording ? 0.40: 1)
  28.                     .frame(width: 58, height: 58)
  29.                     .onTapGesture {
  30.                         withAnimation {
  31.                             isRecording.toggle()
  32.                         }
  33.                     }
  34.             )
  35.             .frame(width: 70, height: 70)
  36.     }
  37. }
  38.  
  39. struct RecordButton: View {
  40.     @State var isRecording = false
  41.     var body: some View {
  42.         Button("", action: {
  43.             withAnimation {
  44.                 isRecording.toggle()
  45.             }
  46.         })
  47.             .buttonStyle(RecordButtonStyle(isRecording: $isRecording))
  48.             .frame(width: 70, height: 70)
  49.     }
  50. }
  51.    
  52. struct PhotoButton: View {
  53.     var body: some View {
  54.         Button("", action: {})
  55.             .buttonStyle(PhotoButtonStyle())
  56.             .frame(width: 70, height: 70)
  57.            
  58.     }
  59. }
  60.  
  61.  
  62. struct RecordButtonStyle: ButtonStyle {
  63.     @Binding var isRecording: Bool
  64.    
  65.     func makeBody(configuration: Configuration) -> some View {
  66.         configuration.label
  67.             .background(
  68.                 Circle()
  69.                     .strokeBorder(Color.white, lineWidth: 4.0)
  70.                     .frame(width: 70, height: 70)
  71.                     .overlay(
  72.                         RoundedRectangle(cornerRadius: isRecording ? 10: 50)
  73.                             .fill(Color.red)
  74.                             .scaleEffect(isRecording ? 0.40: 1)
  75.                             .frame(width: 58, height: 58)
  76.                     )
  77.             )
  78.     }
  79. }
  80.  
  81. struct PhotoButtonStyle: ButtonStyle {
  82.     func makeBody(configuration: Configuration) -> some View {
  83.         configuration.label
  84.             .background(
  85.                 Circle()
  86.                     .strokeBorder(Color.white, lineWidth: 4.0)
  87.                     .frame(width: 70, height: 70)
  88.                     .overlay(
  89.                         Circle()
  90.                             .fill(Color.white)
  91.                             .scaleEffect(configuration.isPressed ? 0.95: 1)
  92.                             .frame(width: 58, height: 58)
  93.                     )
  94.             )
  95.     }
  96. }
  97.  
  98. struct RecordButtons_Previews: PreviewProvider {
  99.     static var previews: some View {
  100.         RecordButtons()
  101.     }
  102. }
  103.  
Advertisement
Add Comment
Please, Sign In to add comment