Ok thanks for the answer.i assume that I would have my desired behavior if I set the duration for long press gesture to zero.is it possible to pass callback functions to the primitive style?
Post
Replies
Boosts
Views
Activity
Thanks for the snippet. This helped alot. Regarding callbacks I was thinking of something like this:Unfortunately, the code does not compile.Is it possible to extend this Button/Style with additional parameters for callbacks?import SwiftUI
struct ButtonView: View {
var body: some View {
VStack {
Button(actionPressed: {
print("MyNewPrimitiveButton triggered. Is it printed ?")
}, actionReleased: { Do something else}){ Text("My NEW primitive Button").padding() }
.buttonStyle(MyNewPrimitiveButtonStyle(color: .yellow))
}
}
}
struct MyNewPrimitiveButtonStyle: PrimitiveButtonStyle {
var color: Color
func makeBody(configuration: PrimitiveButtonStyle.Configuration) -> some View {
MyButton(configuration: configuration, color: color)
}
struct MyButton: View {
@State private var pressed = false
let configuration: PrimitiveButtonStyle.Configuration
let color: Color
let actionPressed
let actionReleased
var body: some View {
return configuration.label
.foregroundColor(.white)
.padding(15)
.background(RoundedRectangle(cornerRadius: 5).fill(color))
.compositingGroup()
.shadow(color: .black, radius: 3)
.opacity(self.pressed ? 0.5 : 1.0)
.scaleEffect(self.pressed ? 0.9 : 1.0)
.onLongPressGesture(minimumDuration: 2.5, maximumDistance: .infinity, pressing: { pressing in
withAnimation(.easeInOut(duration: 0.5)) {
self.pressed = pressing
}
if pressing {
print("My long pressed starts")
print(" I can initiate any action on start")
self.actionPressed()
} else {
print("My long pressed ends")
print(" I can initiate any action on end")
self.actionReleased()
}
}, perform: { })
}
}
}
struct ButtonView_Previews: PreviewProvider {
static var previews: some View {
ButtonView()
}
}