Using SwiftUI, how can I draw this path only after clicking the button?

60 views Asked by At

Here's my code that draws a black triangle in the main View (see image):

import SwiftUI

struct ShapeView: View {
    
    func drawPath() -> Path{
        Path { path in
            path.move(to: CGPoint(x: 200, y: 100))
            path.addLine(to: CGPoint(x: 100, y: 300))
            path.addLine(to: CGPoint(x: 300, y: 300))
            path.addLine(to: CGPoint(x: 200, y: 100))
        }
        
    }
    
    var body: some View {
        VStack{
            Button("Draw"){
                
            }
            Text("Test")
            drawPath()
        }
        
    }
}

I want to draw the triangle, only after the button is clicked.

How can I do that?
When I add the call to drawPath() to the button click handler, I get an warning and when I click the button it doesn't draw the triangle.

The warning states: "Result of call to 'drawPath()' is unused"

How can I draw the triangle upon clicking the button?

black triangle in main view

0

There are 0 answers