swiftUI3.0制作行星App


核心源码

//
//  ContentView.swift
//  LJPlanets
//
//  Created by lujun on 2022/1/24.
//

import SwiftUI

//导入场景Kit 导入场景框架
import SceneKit

struct ContentView: View {
    
    @State  var currentIndex = 0
    
    var body: some View {
        VStack{
            
            ZStack{
                HStack{
                    Button {
                        withAnimation {
                            if currentIndex > 0 {
                                currentIndex -= 1
                            }
                        }
                        
                    } label: {
                        Image(systemName: "chevron.left")
                            .font(.system(size: 35))
                            .opacity(currentIndex == 0 ? 0.3 : 1)
                    }
                    .disabled(currentIndex == 0 ? true : false)
                    
                    Spacer()
                    
                    Button {
                        
                        withAnimation {
                            if currentIndex < planets.count - 1 {
                                currentIndex += 1
                            }
                        }
                        
                    } label: {
                        Image(systemName: "chevron.right")
                            .font(.system(size: 35))
                            .opacity(currentIndex == (planets.count - 1) ? 0.3 : 1)
                    }
                    .disabled(currentIndex == (planets.count - 1) ? true : false)
                    
                   

                }
                Text(planets[currentIndex].name)
                    .font(.system(size: 45) .bold())
            }
            .foregroundColor(.black)
            .padding(.horizontal)
            .padding(.vertical,30)
               
            SceneView(scene: SCNScene(named: planets[currentIndex].filename),options: [.autoenablesDefaultLighting,.allowsCameraControl])
                .frame(width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.width)
            
                //关于行星
            
            VStack(alignment:.leading,spacing: 15){
                Text("关于")
                    .font(.title.bold())
                + Text(planets[currentIndex].name)
                    .font(.title.bold())
                ScrollView(.vertical, showsIndicators: false) {
                    
                    Text(planets[currentIndex].details)
                    
                }
                    
            }
            .padding()
            
            Spacer()
            
            
            
        }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}