Problem
I’ve been prototyping recently on the iPad using Swift Playgrounds, but I can’t add an image to the project, I click on the plus sign to add an Image then from the camera Roll, but nothing happens.
Parameters
I am on the Public Beta 8 for iPadOS
The project template is “Playgrounds for XCode”
Doesn’t matter where in the code or if in a new file, the error continues
Post
Replies
Boosts
Views
Activity
Ever since the update for Swift 5.3 on Swift Playgrounds.
Using a struct inside another struct, gives an error, same with Navigation View and Navigation Link, I’ve tried a few thing, but nothing worked, and no updates have been realized
When using certain elements the app will tell me that there’s an error, even though there isn’t, the elements are:
List
NavigationView
Spacer
OutlineGroup
I’ve been searching and trying to fix it, but I have not been able to find a solution.
Also the @State variables do not update the UI
I leave my code below, in case theres an error with it and not Swift Playgrounds
// 1 - Import ARKit, RealityKit, PlaygroundSuppor
import ARKit
import RealityKit
import SwiftUI
import PlaygroundSupport
// 2 - Create ARView
let arView = ARView(frame: .zero, cameraMode: .ar, automaticallyConfigureSession: true)
// 3- Create simple AR cube to place in scene
let saturn = MeshResource.generateSphere(radius: 0.15)
let ring = MeshResource.generatePlane(width: 1, depth: 1, cornerRadius: 0.5)
var ringMaterial = SimpleMaterial()
ringMaterial.baseColor = try! MaterialColorParameter.texture(
TextureResource.load(named: "ring.PNG"))
var saturnMaterial = SimpleMaterial()
saturnMaterial.baseColor = try! MaterialColorParameter.texture(
TextureResource.load(named: "2k_saturn.jpg"))
let saturnEntity = ModelEntity(mesh: saturn, materials: [saturnMaterial])
let ringEntity = ModelEntity(mesh: ring, materials: [ringMaterial])
saturnEntity.addChild(ringEntity)
let anchorEntity = AnchorEntity(plane: .horizontal)
anchorEntity.name = "My Cube"
anchorEntity.addChild(saturnEntity)
arView.scene.addAnchor(anchorEntity)
// 4 - Generate collision shapes
saturnEntity.generateCollisionShapes(recursive: true)
// 4(b) - Intall Gestures
arView.installGestures([.translation, .rotation, .scale], for: saturnEntity)
// 5 - Create extensions of ARView to enable longPressGesture to delete AR object
extension ARView {
func enableObjectRemoval() {
let longPressGestureRecognizer = UILongPressGestureRecognizer(target: self, action: #selector(handleLongPress(recognizer:)))
self.addGestureRecognizer(longPressGestureRecognizer)
}
@objc func handleLongPress(recognizer: UILongPressGestureRecognizer) {
let location = recognizer.location(in: self)
if let entity = self.entity(at: location) {
if let anchorEntity = entity.anchor, anchorEntity.name == "My Cube" {
anchorEntity.removeFromParent()
print("Remove anchor" + anchorEntity.name)
}
}
}
}
// 5(b) - Create enableObjectRemoval() function to add longPressGesture recognizer
arView.enableObjectRemoval()
struct ContentView: View {
@State private var showView = false
@State private var selection = 0
@State private var showARView = false
@State var selectedPlanet: PlanetModel
var body: some View {
Button("Show me") {
self.showView.toggle()
}
.fullScreenCover(isPresented: $showView) {
HStack {
ARViewContainer().ignoresSafeArea(.all)
.frame(maxWidth: showARView ? .infinity : 0)
NavigationView {
VStack {
HStack {
selectedPlanet.image
.resizable()
.scaledToFill()
.frame(width: 150, height: 150)
}
List {
ForEach(planets) { planet in
if selectedPlanet.name != planet.name {
NavigationLink(destination: PlanetDetailedView(planet: planet)) {
PlanetView(selectedPlanet: selectedPlanet, thisPlanet: planet)
}
}
}
}
}
.toolbar {
ToolbarItem(placement: .navigationBarLeading) {
Button(action: {
withAnimation {
self.showARView.toggle()
print("Show AR View Toggle: \(showARView)")
}
}) {
Label("Show AR View", systemImage: "arkit")
}
}
}
.navigationBarTitle("Solar System", displayMode: .inline)
}
.navigationViewStyle(StackNavigationViewStyle())
}
}
}
}
struct PlanetView: View {
let selectedPlanet: PlanetModel
let thisPlanet: PlanetModel
var body: some View {
HStack {
thisPlanet.image
.resizable()
.scaledToFill()
.frame(width: 100, height: 100)
Text(thisPlanet.name)
.padding(.leading, 15)
.font(.title)
Spacer()
Button(action: {
}) {
Label("Switch Planet", systemImage: "arrow.left.arrow.right")
}
}
}
}
struct PlanetDetailedView: View {
let planet: PlanetModel
var body: some View {
ScrollView {
planet.image
.resizable()
.scaledToFit()
.frame(height: 150)
}
.navigationBarTitle(planet.name)
}
}
struct ARViewContainer: UIViewRepresentable {
func makeUIView(context: Context) - ARView {
let newARView = ARView(frame: .zero)
return arView
}
func updateUIView(_ uiView: ARView, context: Context) {
}
}
PlaygroundPage.current.setLiveView(ContentView(selectedPlanet: planets[0]))
And the PlanetModel is below.
import SwiftUI
public struct PlanetModel: Identifiable {
public var id = UUID()
public var name: String
public var image: Image
public var distanceToSun: Float
public init(_ name: String, image: UIImage, distanceToSun: Float) {
self.name = name
self.image = Image(uiImage: image)
self.distanceToSun = distanceToSun
}
}
public let planets = [
PlanetModel("Sun", image: imageLiteral(resourceName: "Foto 3.png"), distanceToSun: 0),
PlanetModel("Earth", image: imageLiteral(resourceName: "Foto 1.png"), distanceToSun: 1),
PlanetModel("Mars", image: imageLiteral(resourceName: "Foto.png"), distanceToSun: 1.5),
PlanetModel("Jupiter", image: imageLiteral(resourceName: "Foto 2.png"), distanceToSun: 5.2),
PlanetModel("Saturn", image: imageLiteral(resourceName: "Captura.PNG"), distanceToSun: 9.5)
]
I have been trying to use the new @Binding capabilities in SwiftUI but every time I make a ForEach and pass binding values, when the TextField changes, the keyboard unfocuses the Field.
struct ContentView: View {
@State private var texts: [String] = ["Orange", "Apple"]
var body: some View {
NavigationView {
List {
ForEach($texts, id: \.self) { $text in
TextField("Fruit", text: $text)
}
}
.navigationTitle("Fruit List")
}
.navigationViewStyle(.stack)
}
}
That is what I have tried, is there an error in my implementation or is it just a bug?
My Mac:
MacMini 2020 - M1 16GB
Xcode 13.0 - (13A233)
Project Built - for iOS 15