Open onmyway133 opened 11 months ago
We can use ButtonStyleConfiguration to detect isPressed state
ButtonStyleConfiguration
isPressed
struct RecordButton: View { var body: some View { Button { } label: { Image(systemSymbol: .micFill) } .buttonStyle(RecordButtonStyle()) } } private struct RecordButtonStyle: ButtonStyle { func makeBody(configuration: Configuration) -> some View { configuration.label .background { if configuration.isPressed { Circle() .fill(Color.pink) .frame(square: 30) } } .onChange(of: configuration.isPressed) { isPressed in print("isPressed", isPressed) } } }
Alternatively, you can just use a DragGesture
Image(systemSymbol: .micFill) .foregroundStyle(.tint) .background { if isPressed { Circle() .fill(Color.pink.opacity(0.8)) .frame(square: 50) } } .simultaneousGesture( DragGesture(minimumDistance: 0) .onChanged { _ in print("press") } .onEnded { _ in print("release") } )
We also use Button with LongPressGesture using its onEnded modifier to detect when holding begins
Button
LongPressGesture
onEnded
Button { print("end") } label: { Image(systemSymbol: .micFill) .foregroundStyle(.tint) .background { if isPressed { Circle() .fill(Color.pink.opacity(0.8)) .frame(square: 50) } } } .simultaneousGesture( LongPressGesture(minimumDuration: 0.1) .onEnded { _ in print("start") } )
We can use
ButtonStyleConfiguration
to detectisPressed
stateAlternatively, you can just use a DragGesture
We also use
Button
withLongPressGesture
using itsonEnded
modifier to detect when holding begins