why it cannot show anything in playground in swift 2?

爱⌒轻易说出口 提交于 2019-12-13 15:12:30

问题


I try to use spriteKit to show something in playground in assistant editor. However, nothing showed. Below is the code. And if any one can show the results (a blue rectangle), please inform me. If not, please figure out where the problem is.

import UIKit
import SpriteKit

let view:SKView = SKView(frame: CGRectMake(0, 0, 1000, 800))
let scene:SKScene = SKScene(size: CGSizeMake(1000, 800))
scene.scaleMode = SKSceneScaleMode.AspectFit

let blueBox: SKSpriteNode = SKSpriteNode(color: UIColor.blueColor(), size: CGSizeMake(300, 300))
blueBox.position = CGPointMake(512, 384)
scene.addChild(blueBox)

view.presentScene(scene)

回答1:


You can see the current state of a view by clicking the QuickLook (eye) or circled-plus button in the sidebar. But to see your SpriteKit scene animate, you probably want a live view. For that, you need the XCPlayground framework.

import XCPlayground
XCPShowView("my SpriteKit view", view)

For details, read up on Exploring and Evaluating Swift Code in a Playground in Xcode Help or watch WWDC 2014 session 408: Swift Playgrounds.




回答2:


As XCPShowView was deprecated in Xcode 7.1, use PlaygroundPage in PlaygroundSupport instead. The following is an example of using SpriteKit in Playground which is inspired from this.

import SpriteKit
import PlaygroundSupport

// Create the SpriteKit View
let view = SKView(frame: CGRect(x: 0, y: 0, width: 500, height: 500))

// Create the scene and add it to the view
let scene = SKScene(size: CGSize(width: 500, height: 500))
scene.scaleMode = SKSceneScaleMode.aspectFit
scene.backgroundColor = .white
view.presentScene(scene)

// Add a red box to the scene
let redBox = SKSpriteNode(color: SKColor.red, size: CGSize(width: 200, height: 200))
redBox.position = CGPoint(x: 250, y: 250)
redBox.run(SKAction.repeatForever(SKAction.rotate(byAngle: -5, duration: 5)))
scene.addChild(redBox)

// Show in assistant editor
PlaygroundPage.current.liveView = view
PlaygroundPage.current.needsIndefiniteExecution = true


来源:https://stackoverflow.com/questions/24317153/why-it-cannot-show-anything-in-playground-in-swift-2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!