Con*_*sed 5 gradient ios sprite-kit swift skkeyframesequence
SKKeyframeSequence上的Apple文档提供了用于创建渐变的简短示例代码:
let colorSequence = SKKeyframeSequence(keyframeValues: [SKColor.green,
SKColor.yellow,
SKColor.red,
SKColor.blue],
times: [0, 0.25, 0.5, 1])
colorSequence.interpolationMode = .linear
stride(from: 0, to: 1, by: 0.001).forEach {
let color = colorSequence.sample(atTime: CGFloat($0)) as! SKColor
}
Run Code Online (Sandbox Code Playgroud)
当与某种绘图系统结合使用时,据说输出:
如何从演示代码中的颜色序列采样中得出?
ps我没有任何线索如何使用SpriteKit对象绘制它,因此没有尝试代码.我不是要求代码,只是如何使用这个'数组'颜色来创建一个可以在SpriteKit中用作纹理的渐变的答案.
由于某些原因,颜色是不同的,但这是我使用他们的源代码提出的:
PG设置:
import SpriteKit
import PlaygroundSupport
let sceneView = SKView(frame: CGRect(origin: CGPoint.zero, size: CGSize(width: 1000, height: 450)))
let scene = SKScene(size: CGSize(width: 1000, height: 450))
LOADSCENE: do {
scene.backgroundColor = .white
scene.anchorPoint = CGPoint(x: 0, y: 0.5)
scene.physicsWorld.gravity = CGVector.zero
sceneView.presentScene(scene)
PlaygroundPage.current.liveView = sceneView
}
Run Code Online (Sandbox Code Playgroud)
解:
// Utility func:
func drawLine(from point1: CGPoint, to point2: CGPoint, color: SKColor) {
let linePath = CGMutablePath()
linePath.move(to: point1)
linePath.addLine(to: point2)
let newLine = SKShapeNode(path: linePath)
newLine.strokeColor = color
newLine.lineWidth = 1
newLine.zPosition = 10
scene.addChild(newLine)
newLine.position.x = point1.x
}
// Holds our soon-to-be-generated colors:
var colors = [SKColor]()
LOADCOLORS: do {
let colorSequence = SKKeyframeSequence(keyframeValues: [SKColor.green,
SKColor.yellow,
SKColor.red,
SKColor.blue],
times: [0, 0.25, 0.5, 1])
colorSequence.interpolationMode = .linear
stride(from: 0, to: 1, by: 0.001).forEach {
colors.append(colorSequence.sample(atTime: CGFloat($0)) as! SKColor)
}
}
DRAWGRAD: do {
for i in 1...999 {
let p1 = CGPoint(x: CGFloat(i), y: scene.frame.minY)
let p2 = CGPoint(x: CGFloat(i), y: scene.frame.maxY)
drawLine(from: p1, to: p2, color: colors[i])
}
print("Give me my 25 cookie points, please and TY")
}
Run Code Online (Sandbox Code Playgroud)
然后你应该能够将其作为纹理:
let texture = sceneView.texture(from: scene)
Run Code Online (Sandbox Code Playgroud)
由于某种原因,渲染这个花了大约一百万年来在我的gen2 i5上以2.6ghz渲染.将不得不调查,除非它只是一个PG错误......
| 归档时间: |
|
| 查看次数: |
364 次 |
| 最近记录: |