Skip to content

Instantly share code, notes, and snippets.

@AdamWhitcroft
Last active August 9, 2023 22:55
Show Gist options
  • Save AdamWhitcroft/b5fe9b03738442bb6609529c7c985b52 to your computer and use it in GitHub Desktop.
Save AdamWhitcroft/b5fe9b03738442bb6609529c7c985b52 to your computer and use it in GitHub Desktop.
//
// Thanks for offering to help! This is where I'm at now.
// It's an incredibly simple scene, but I'm just starting to
// poke around with Scenekit.
//
// The TL;DR of what I'm trying to do is:
// Having a moon in the middle of the view
// User CAN rotate moon (currently working)
// User CANNOT pinch to zoom (currently working)
// User CANNOT reposition the moon within the view (currently NOT WORKING)
//
import SwiftUI
import SceneKit
struct SceneKitThings: View {
var body: some View {
// Set up your Scene here (replace the placeholder with your scene setup)
let scene = SCNScene(named: "Moon.usdz")!
// Use the MySceneViewWrapper and pass your SCNScene as a parameter
MySceneViewWrapper(scene: scene)
.edgesIgnoringSafeArea(.all)
.frame(maxWidth: UIScreen.main.bounds.width, maxHeight: UIScreen.main.bounds.height / 2)
}
}
struct MySceneViewWrapper: UIViewRepresentable {
let scene: SCNScene // Pass your SCNScene as a parameter
func makeUIView(context: Context) -> SCNView {
let sceneView = SCNView()
sceneView.scene = scene // Set the SCNScene to the SCNView
sceneView.autoenablesDefaultLighting = true // Enable default lighting
sceneView.allowsCameraControl = true // Enable camera control for rotation
// Disable pinch-to-zoom by removing the pinch gesture recognizer
sceneView.gestureRecognizers?.removeAll { $0 is UIPinchGestureRecognizer }
return sceneView
}
func updateUIView(_ uiView: SCNView, context: Context) {
// Update your Scene here if needed
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment