Mac OS的Playgrounds应用存在问题,如何解决?

I'm making simple app and using SceneKit, SpriteKit and UIKit in Xcode playground. When I trying to build my app in xcode it works fine (this is a screenshoot)

But when I opening the same project in Playgrounds app for MacOS there is some problems. First problem is an error that appears when I run my project - (error)

我已经知道如何解决,它禁用了开关-启用结果。但我想知道它为什么出现。

Second problem is that the project in the simulator looks different. It's looks like this - (enter image description here). I wanna know why.

这是我的代码:

我用-Introduction()运行项目

import PlaygroundSupport
import SceneKit
import SpriteKit

public class Introduction {

    public init() {
        let introVC = IntroViewController()
        PlaygroundPage.current.liveView = introVC
        PlaygroundPage.current.needsIndefiniteExecution = true
    }

    class IntroViewController: UIViewController {

        var sceneView: SCNView!
        var scene: SCNScene!
        var cameraNode: SCNNode!
        var field: SCNPhysicsField!
        var fieldNode: SCNNode!
        var letsgoNode: SCNNode!
        var introSKScene: IntroSKScene!

        override func viewDidLoad() {
            super.viewDidLoad()

            sceneView = SCNView(frame: CGRect(x: 0, y: 0, width: view.frame.width, height: view.frame.height))
            view.addSubview(sceneView)

            scene = SCNScene()
            sceneView.scene = scene
            sceneView.backgroundColor = .black

            cameraNode = SCNNode()
            cameraNode.physicsBody = SCNPhysicsBody.dynamic()
            cameraNode.camera = SCNCamera()
            cameraNode.camera?.zFar = 10000
            cameraNode.position = SCNVector3(x: 0, y: 0, z: 450)
            scene.rootNode.addChildNode(cameraNode)

            var shape = SCNSphere(radius: 420)

            func configure(system: SCNParticleSystem, colors: [UIColor]) {
                for color in colors {
                    system.loops = true
                    system.birthRate = 500
                    system.emissionDuration = 5
                    system.emitterShape = shape
                    system.particleLifeSpan = 10
                    system.particleSize = 0.4
                    system.particleColor = color
                    system.isAffectedByPhysicsFields = true
                    system.isAffectedByGravity = false
                    scene.addParticleSystem(system, transform: SCNMatrix4MakeRotation(0, 0, 0, 0))
                }
            }

            configure(system: SCNParticleSystem(), colors: [UIColor.yellow])

            field = SCNPhysicsField.vortex()
            field.strength = -0.5
            fieldNode = SCNNode()
            fieldNode.physicsField = field

            showFirstScene()
        }

        // MARK: - Preparing Scenes

        func showFirstScene() {
            let letsgoText = SCNText(string: "There are about 51 galaxies \nin the Local Group, \non the order of 100,000 \nin our Local Supercluster, \nand an estimated one to \ntwo trillion \nin all of the observable universe.", extrusionDepth: 0)
            let font = UIFont(name: "Futura", size: 10)
            letsgoText.font = font
            letsgoText.alignmentMode = CATextLayerAlignmentMode.center.rawValue
            letsgoText.firstMaterial?.emission.contents = UIColor.white

            let (minBound, maxBound) = letsgoText.boundingBox
            letsgoNode = SCNNode(geometry: letsgoText)
            letsgoNode.opacity = 0.0
            letsgoNode.pivot = SCNMatrix4MakeTranslation( (maxBound.x - minBound.x)/2, minBound.y, 0.02/2)
            letsgoNode.scale = SCNVector3Make(1, 1, 1)
            letsgoNode.position = SCNVector3(0, 0, 200)

            Timer.scheduledTimer(withTimeInterval: 0.5, repeats: false) { (timer) in
                self.letsgoNode.runAction(SCNAction.sequence([SCNAction.wait(duration: 2), SCNAction.fadeIn(duration: 4)]))
            }

            introSKScene = IntroSKScene(intro: self)
            sceneView.overlaySKScene = introSKScene

            Timer.scheduledTimer(withTimeInterval: 4, repeats: false) { (timer) in
                self.introSKScene.startLabel.run(SKAction.fadeIn(withDuration: 0.5))
                self.introSKScene.startBackground.run(SKAction.fadeIn(withDuration: 0.5))
                self.introSKScene.firstAnimationIsWatched = true
            }

            scene.rootNode.addChildNode(letsgoNode)
        }

        func showSecondScene() {
            let letsgoText = SCNText(string: "We are going to look \nonly on those galaxies \nthat are well-studied \nby humans. \nWe will learn how \nthey look like \nand some information \nabout them.", extrusionDepth: 0)
            let font = UIFont(name: "Futura", size: 10)
            letsgoText.font = font
            letsgoText.alignmentMode = CATextLayerAlignmentMode.center.rawValue
            letsgoText.firstMaterial?.emission.contents = UIColor.white

            let (minBound, maxBound) = letsgoText.boundingBox
            letsgoNode = SCNNode(geometry: letsgoText)
            letsgoNode.opacity = 0.0
            letsgoNode.pivot = SCNMatrix4MakeTranslation( (maxBound.x - minBound.x)/2, minBound.y, 0.02/2)
            letsgoNode.scale = SCNVector3Make(1, 1, 1)
            letsgoNode.position = SCNVector3(0, 0, 0)

            Timer.scheduledTimer(withTimeInterval: 0.5, repeats: false) { (timer) in
                self.letsgoNode.runAction(SCNAction.sequence([SCNAction.wait(duration: 2), SCNAction.fadeIn(duration: 4)]))
            }

            introSKScene = IntroSKScene(intro: self)
            sceneView.overlaySKScene = introSKScene

            Timer.scheduledTimer(withTimeInterval: 4, repeats: false) { (timer) in
                self.introSKScene.startLabel.run(SKAction.fadeIn(withDuration: 0.5))
                self.introSKScene.startBackground.run(SKAction.fadeIn(withDuration: 0.5))
                self.introSKScene.secondAnimationIsWatched = true
            }

            scene.rootNode.addChildNode(letsgoNode)
        }

        func showThirdScene() {
            let letsgoText = SCNText(string: "Let's Explore", extrusionDepth: 0)
            let font = UIFont(name: "Futura", size: 20)
            letsgoText.font = font
            letsgoText.alignmentMode = CATextLayerAlignmentMode.center.rawValue
            letsgoText.firstMaterial?.emission.contents = UIColor.white

            let (minBound, maxBound) = letsgoText.boundingBox
            letsgoNode = SCNNode(geometry: letsgoText)
            letsgoNode.opacity = 0.0
            letsgoNode.pivot = SCNMatrix4MakeTranslation( (maxBound.x - minBound.x)/2, minBound.y, 0.02/2)
            letsgoNode.scale = SCNVector3Make(1, 1, 1)
            letsgoNode.position = SCNVector3(0, 0, -200)

            Timer.scheduledTimer(withTimeInterval: 0.5, repeats: false) { (timer) in
                self.letsgoNode.runAction(SCNAction.sequence([SCNAction.wait(duration: 2), SCNAction.fadeIn(duration: 4)]))
            }

            introSKScene = IntroSKScene(intro: self)
            sceneView.overlaySKScene = introSKScene

            Timer.scheduledTimer(withTimeInterval: 4, repeats: false) { (timer) in
                self.introSKScene.startLabel.run(SKAction.fadeIn(withDuration: 0.5))
                self.introSKScene.startBackground.run(SKAction.fadeIn(withDuration: 0.5))
                self.introSKScene.thirdAnimationIsWatched = true
            }

            scene.rootNode.addChildNode(letsgoNode)
        }

        // MARK: - For Moving Camera

        func moveCameraFirstTime() {
            self.field = SCNPhysicsField.vortex()
            self.field.strength = 300
            self.fieldNode.physicsField = self.field

            Timer.scheduledTimer(withTimeInterval: 0.1, repeats: false) { (timer) in
                let camMove = SCNAction.move(to: SCNVector3Make(0, 0, 200), duration: 2)
                camMove.timingMode = .easeIn
                self.cameraNode.runAction(SCNAction.sequence([SCNAction.wait(duration: 0), camMove]))
                self.showSecondScene()
            }
        }

        func moveCameraSecondTime() {
            self.field = SCNPhysicsField.vortex()
            self.field.strength = 300
            self.fieldNode.physicsField = self.field

            Timer.scheduledTimer(withTimeInterval: 0.1, repeats: false) { (timer) in
                let camMove = SCNAction.move(to: SCNVector3Make(0, 0, 0), duration: 2)
                camMove.timingMode = .easeIn
                self.cameraNode.runAction(SCNAction.sequence([SCNAction.wait(duration: 0), camMove]))
                self.showThirdScene()
            }
        }

        func moveCameraThirdTime() {
            self.field = SCNPhysicsField.vortex()
            self.field.strength = 300
            self.fieldNode.physicsField = self.field

            Timer.scheduledTimer(withTimeInterval: 0.1, repeats: false) { (timer) in
                let camMove = SCNAction.move(to: SCNVector3Make(0, 0, -200), duration: 2)
                camMove.timingMode = .easeIn
                self.cameraNode.runAction(SCNAction.sequence([SCNAction.wait(duration: 0), camMove]))
                self.introSKScene.blackFadeOut()

                Timer.scheduledTimer(withTimeInterval: 2, repeats: false, block: { (t) in
                    print("The End")
    //                self.loadGalaxiesView()
                })
            }
        }

        // MARK: - Load GalaxiesView

        func loadGalaxiesView() {

        }
    }

    // MARK: - Intro Control

    class IntroSKScene: SKScene {

        let intro: IntroViewController
        let startLabel: SKLabelNode
        var startBackground: SKShapeNode!

        init(intro: IntroViewController) {
            self.intro = intro
            self.startLabel = SKLabelNode(text: "Next!")
            super.init(size: intro.sceneView.frame.size)
            let font = UIFont(name: "Futura", size: 14)!.fontName
            startLabel.verticalAlignmentMode = .center
            startLabel.horizontalAlignmentMode = .center
            scaleMode = .aspectFit
            self.startBackground = SKShapeNode(rect: CGRect(origin: CGPoint.zero, size: CGSize(width: 1000, height: startLabel.frame.height)), cornerRadius: 1.0)
            startBackground.strokeColor = UIColor(white: 1, alpha: 0.0)
            startBackground.alpha = 0.0
            addChild(startBackground)
            startLabel.fontSize = 20
            startLabel.alpha = 0
            startLabel.fontName = font
            addChild(startLabel)
            updatePositions()
        }

        func blackFadeOut() {
            let background = SKShapeNode(rectOf: CGSize(width: 5000, height: 5000))
            background.fillColor = UIColor.black
            background.strokeColor = UIColor.black
            background.alpha = 0.0
            background.zPosition = 5
            startBackground.run(SKAction.fadeOut(withDuration: 0.2))
            addChild(background)
            background.run(SKAction.fadeIn(withDuration: 2))
        }

        var isStarted = false
        var firstAnimationIsWatched = false
        var secondAnimationIsWatched = false
        var thirdAnimationIsWatched = false

        override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
            if firstAnimationIsWatched == false {
                if secondAnimationIsWatched == false {
                    if thirdAnimationIsWatched == false {
                        return
                    } else {
                        if let _ = touches.first, !isStarted {
                            startLabel.run(SKAction.fadeOut(withDuration: 0.5))
                            startBackground.run(SKAction.fadeOut(withDuration: 0.5))
                            self.intro.moveCameraThirdTime()
                            thirdAnimationIsWatched = false
                        }
                    }
                } else {
                    if let _ = touches.first, !isStarted {
                        startLabel.run(SKAction.fadeOut(withDuration: 0.5))
                        startBackground.run(SKAction.fadeOut(withDuration: 0.5))
                        self.intro.moveCameraSecondTime()
                    }
                }
            } else {
                if let _ = touches.first, !isStarted {
                    startLabel.run(SKAction.fadeOut(withDuration: 0.5))
                    startBackground.run(SKAction.fadeOut(withDuration: 0.5))
                    self.intro.moveCameraFirstTime()
                }
            }
        }

        func updatePositions() {
            startLabel.position = CGPoint(x: frame.midX, y: frame.midY - 40 - startBackground.frame.height / 2)
            startBackground.position = CGPoint(x: frame.midX - startBackground.frame.width / 2, y: frame.midY - startBackground.frame.height / 2 - 40)
        }

        required init?(coder aDecoder: NSCoder) {
            fatalError("init(coder:) has not been implemented")
        }
    }
}

谢谢你的帮助 !!!

评论