2017-01-24 23 views
5

SKKeyframeSequence蘋果的文檔有旨在創建一個梯度簡短的示例代碼:如何繪製梯度SKKeyframeSequence:根據蘋果文檔

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中用作紋理的漸變的答案。

+0

你不知道如何把它放到CALayer?有一種簡單的方法可以讓CALayer進入SKTexture ...我想如果我檢查我的筆記... – Fluidity

+0

不,我不知道該怎麼做。前進。檢查你的筆記。 @流利你一定很聰明。 – Confused

+0

但是,在這之間,請花些時間重新考慮問題。這是問如何在.... SPRITEKIT中做到這一點。不是核心動畫。 – Confused

回答

5

的顏色是由於某種原因不同,但這裏是我想出了用自己的源代碼:

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) 

渲染這需要大約一百萬年來呈現在我的gen2 i5 2.6ghz出於某種原因。將不得不考慮,除非它只是一個PG錯誤...