SALE: Save 50% on all my books and bundles >>

Add a view by tapping the screen

Forums > SwiftUI

Hi,

I having a hard time trying to add a view to a view. I like the user to tap the screen of an iPad and a view show up. If repeted, more views show up on the location the user tapped. I like to build an array that stores the data for the views as they are created.

The data model will include some id, some text, location.

I put everything in a ZStack and onTapGesture and I can add items to the array.

The problem is in displaying the items by go through the array with a ForEach , I can't make it work...

Any tip world be appreciated!

/Richard

Ps. I have gone through the Hacking with IOS SwiftUI, but I don't have any other experience...

   

Some code examples showing what you've done would be helpful.

   

Here is my code that don't work.

/Richard

import SwiftUI

struct ContentView: View {
@State private var dataArray = [Data]()
@State private var pos = CGPoint(x: 10, y: 10)
@State private var offset = CGFloat(50)

struct Data: Identifiable {
    let id = UUID()
    var text = String("Start text")
    var pos: CGPoint = CGPoint(x: 10, y: 10)
}

var body: some View {
    ZStack {
        GeometryReader { geo in
            if self.dataArray.isEmpty {
                Text("Klick to add a text")
                    .frame(width: geo.size.width, height: geo.size.height)
                    .background(Color.white)
                .onTapGesture(count: 2) {
                print("Tapped")
                self.dataArray.append(Data(text: "New line", pos: CGPoint(x: geo.frame(in: .global).midX, y: geo.frame(in: .global).midY)))
                self.offset += 20
                    print(self.dataArray)}

            } else {
                ForEach(self.dataArray) { data in
                    Text(data.text)
                    .frame(width: geo.size.width, height: geo.size.height)

// .background(Color.red) .position(x: data.pos.x + self.offset, y: data.pos.y) .gesture(DragGesture() .onChanged( { value in

                            self.pos = value.location
                        }))

                }.onTapGesture(count: 2) {
                    print("Tapped")
                    self.dataArray.append(Data(text: "New line", pos: CGPoint(x: geo.frame(in: .global).midX, y: geo.frame(in: .global).midY)))
                    self.offset += 20
                    print(self.dataArray)
            }
        }

    }

    }
}

}

   

Hey! I found the solution! Here is a link to how to do it! I took some time to figure it out so I woul always appreciate a subscribe on YouTube in return if I helped you :) Link: https://www.youtube.com/watch?v=x3mzLhPfLaE&feature=youtu.be

   

Here is the link to how to do it! I took some time to figure it out so I woul always appreciate a subscribe on YouTube in return if I helped you :) Link: https://www.youtube.com/watch?v=x3mzLhPfLaE&feature=youtu.be

   

Thanks for the tip cag116! This will help a lot!

/Richard

   

Hacking with Swift is sponsored by RevenueCat

SPONSORED Building in-app subscriptions are hard. RevenueCat makes it simple. With their open source SDKs, you can painlessly implement subscriptions for your app in hours, not months.

Explore the docs to learn more

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

Snapthread is a casual video editor and slideshow maker that makes discovering, compiling and sharing your favorite memories effortless.

 
Unknown user

Not logged in

Log in