如何在Swift游乐场获得画布

时间:2022-06-01 14:03:42

I would like to get a canvas that I can use to draw on. The target would be an iOS based Swift playground. I searched the documentation and I could not found an object named Canvas, but if there is something similar to it then it would be good for me as well.

我想得到一个可以用来画画的画布。目标将是基于iOS的Swift游乐场。我搜索了文档,我找不到一个名为Canvas的对象,但如果有类似的东西那么它对我也有好处。

1 个解决方案

#1


7  

You can use something similar to this, note you don't have to use Sprite Kit classes.

你可以使用类似的东西,注意你不必使用Sprite Kit类。

import UIKit 
import PlaygroundSupport 
import SpriteKit 


//    Playground Container Setup 
// 
// 
let containerWidth: CGFloat = 667.0 
let containerHeight: CGFloat = 375.0 
let containerCenter: CGPoint = CGPoint(x: (containerWidth/2), y: (containerHeight/2)) 


let containerView = SKView(frame: CGRect(x: 0.0, y: 0.0, width: containerWidth, height: containerHeight)) 


PlaygroundPage.current.liveView = containerView 


let containterScene: SKScene = SKScene(size: CGSize(width: containerWidth, height: containerHeight)) 
containerView.presentScene(containterScene) 

UPDATED

I've created a wrapper class to make this easier for everyone. Download the Playground here on github

我已经创建了一个包装类,使每个人都更容易。在github上下载Playground

#1


7  

You can use something similar to this, note you don't have to use Sprite Kit classes.

你可以使用类似的东西,注意你不必使用Sprite Kit类。

import UIKit 
import PlaygroundSupport 
import SpriteKit 


//    Playground Container Setup 
// 
// 
let containerWidth: CGFloat = 667.0 
let containerHeight: CGFloat = 375.0 
let containerCenter: CGPoint = CGPoint(x: (containerWidth/2), y: (containerHeight/2)) 


let containerView = SKView(frame: CGRect(x: 0.0, y: 0.0, width: containerWidth, height: containerHeight)) 


PlaygroundPage.current.liveView = containerView 


let containterScene: SKScene = SKScene(size: CGSize(width: containerWidth, height: containerHeight)) 
containerView.presentScene(containterScene) 

UPDATED

I've created a wrapper class to make this easier for everyone. Download the Playground here on github

我已经创建了一个包装类,使每个人都更容易。在github上下载Playground