Learn Courses My Dashboard

On press button do else do swiftui button code help

Hi codecrew
I need to know how to make some logic using swiftui’s button element

What I want todo is if you press a button, then you execute something. If you release the button, then you do something else.

Looking for some help on this.


Are you using SwiftUI or UIKit?

Thanks for reaching out. As mentioned, the button is used in SwiftUI.

This article may be of assistance:

Try this simple View using the code from that article:

struct ContentView: View {
    @State private var isPressed = false

    var body: some View {
        VStack {
            ZStack {
                RoundedRectangle(cornerRadius: 10)
                    .fill(isPressed ? Color.green : Color.blue)
                    .frame(width: 200, height: 50)
                Text(isPressed ? "Release me" : "Press me")
                DragGesture(minimumDistance: 0)
                    .onChanged({ _ in
                        isPressed = true
                    .onEnded({ _ in
                        isPressed = false

My bad I totally didn’t see what when I read it the first time :joy: