0

Goal: a toolbar that switches states, according to object selected (image, text, shape)

What I did:

iimport SwiftUI

struct ToolbarMaster : View {

    @State var showtoolbar = false
    var toolbarmaster:  [ToolbarBezier] =  []

    var body: some View {


        HStack {

            Spacer()

            VStack {

                Button(action: {self.showtoolbar.toggle() }) {
                    Image(systemName: "gear")
                    }
                    .padding(.leading)

                Image("dog")

                Text("Im a text")
                    .font(.largeTitle)
                    .color(.black)


                Path(ellipseIn: CGRect(x: 0, y: 0, width: 100, height: 100))
                .fill(Color.black)

            }


            NavigationView {

                ZStack {

                    ToolbarBezier()

                    ToolbarArtwork()





                }


                .navigationBarTitle(Text("Toolbar Name"), displayMode: .inline)
                }


                .frame(width: 320.0)

        }

    }

}

My result: enter image description here

How can I change the state, while selecting different objects?

I need to do in a dynamic way (not hardcoded), so that when any object that is an image, it will display Image Toolbar, and so on.

Mane Manero
  • 3,086
  • 5
  • 25
  • 47
  • 1
    What do you mean by "selected"? Do you want the toolbar's state to change whenever any of those objects are tapped? – RPatel99 Jun 24 '19 at 23:57
  • Yes. Like, whenever a text is tapped, the text toolbar state shows. Whenever any image is tapped, the image toolbar shows – Mane Manero Jun 25 '19 at 00:12

1 Answers1

0

I would do it like this :

  • Add a @State boolean variable for each state of your toolbar
@State private var textSelected = false
  • Add an .onTap event to each of the "active" views on your scene :
Text("Tap me!")
    .tapAction {
        self.textSelected = true
    }
  • Make the appearance of you toolbar change based on the "state" variable(s)
if self.textSelected {
   self.showTextToolbarContent() 
}

Of course, showTextToolbarContent() is a local function returning a some View with the toolbar content tailored for text selection.

This is obviously only a bare-bone example. In your actual implementation you'll probably use an array of bools for the "selection" state and another state var for the view currently selected.

Bogdan Farca
  • 3,856
  • 26
  • 38