gpt4 book ai didi

ios - 如何用SKKeyframeSequence绘制渐变 : as per Apple docs

转载 作者:搜寻专家 更新时间:2023-10-30 23:05:12 24 4
gpt4 key购买 nike

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
}

当与某种绘图系统结合使用时,据说输出如下: enter image description here如何从演示代码中的颜色序列采样中得出这一点?

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
}

解决方法:

// 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")
}

enter image description here

然后您应该能够将其作为纹理获取:

let texture = sceneView.texture(from: scene)

出于某种原因,在我的 2.6ghz 的 gen2 i5 上渲染这花了大约一百万年的时间。将不得不对此进行调查,除非它只是一个 PG 错误......

关于ios - 如何用SKKeyframeSequence绘制渐变 : as per Apple docs,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41833198/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com