UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

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...

2      

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

2      

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)
            }
        }

    }

    }
}

}

2      

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

2      

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

2      

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

/Richard

2      

BUILD THE ULTIMATE PORTFOLIO APP Most Swift tutorials help you solve one specific problem, but in my Ultimate Portfolio App series I show you how to get all the best practices into a single app: architecture, testing, performance, accessibility, localization, project organization, and so much more, all while building a SwiftUI app that works on iOS, macOS and watchOS.

Get it on Hacking with Swift+

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

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.