for example :how to change the image border color when i click a button
How to change the style of image with a event in swiftui?
Here is an example on how to change a button size when tapped.
struct MyPrimitiveButtonStyle: PrimitiveButtonStyle {
var color: Color
func makeBody(configuration: PrimitiveButtonStyle.Configuration) -> some View {
MyButton(configuration: configuration, color: color)
}
struct MyButton: View {
@GestureState private var pressed = false
let configuration: PrimitiveButtonStyle.Configuration
let color: Color
var body: some View {
let longPress = LongPressGesture(minimumDuration: .infinity, maximumDistance: .infinity)
.updating($pressed) { value, state, _ in state = value }
.onEnded { _ in
self.configuration.trigger()
}
return configuration.label
.foregroundColor(.white)
.padding(15)
.background(RoundedRectangle(cornerRadius: 5).fill(color))
.compositingGroup()
.shadow(color: .black, radius: 3)
.opacity(pressed ? 0.5 : 1.0)
.scaleEffect(pressed ? 0.8 : 1.0)
.gesture(longPress)
}
}
}
Called as:
Button(action: {
}){ Text("My primitive Button").padding() }
.buttonStyle(MyPrimitiveButtonStyle(color: .red))
If you want to change another object, you should use @EnvironmentObject and @Published.
This tutorial should help:
h ttps://www.hackingwithswift.com/quick-start/swiftui/how-to-use-environmentobject-to-share-data-between-views