Issue #929
We can use ButtonStyleConfiguration
to detect isPressed
state
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 {
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")
}
)