SpriteKit
Swift3.0

SpriteKitでグラフ描画

備忘録
SpriteKitで、折れ線グラフの描画を初期プロトタイプした時のメモプログラム。

import SpriteKit
import AudioKit

class VisionScene: SKScene {

    //-----------------Draw Lines
    var lineNode: SKShapeNode!
    var linePoints = [CGPoint]()

    /// Sceneが表示された時に呼ばれる
    override func didMove(to view: SKView) {
        self.initializeLine()

        linePoints.append(CGPoint(x:0, y:0));
        linePoints.append(CGPoint(x:0, y:10));
        linePoints.append(CGPoint(x:0, y:20));
        linePoints.append(CGPoint(x:0, y:30));
        linePoints.append(CGPoint(x:0, y:40));
        linePoints.append(CGPoint(x:44.0, y:-187.0));
        linePoints.append(CGPoint(x:55.5, y:-73.5));
        linePoints.append(CGPoint(x:66.5, y:-44.5));
        linePoints.append(CGPoint(x:77.0, y:-38.5));
        linePoints.append(CGPoint(x:88.5, y:-46.5));
        linePoints.append(CGPoint(x:99.5, y:-46.5));
        linePoints.append(CGPoint(x:110.5, y:-46.5));
        linePoints.append(CGPoint(x:120.5, y:-46.5));
        linePoints.append(CGPoint(x:130.5, y:-46.5));
        linePoints.append(CGPoint(x:140.5, y:46.5));
        linePoints.append(CGPoint(x:150.5, y:36.5));
        linePoints.append(CGPoint(x:160.5, y:26.5));
        self.drawLines(linePoints: linePoints)
    }

    func initializeLine(){
        lineNode = SKShapeNode()
        lineNode.zPosition = 2
        lineNode.strokeColor = UIColor.red
        lineNode.lineWidth = 9
        addChild(lineNode)
    }

    func drawLines(linePoints: Array<Any>) {

        if linePoints.count < 2 {
            activeSliceBG.path = nil
            return
        }

        let path = UIBezierPath()
        path.move(to: linePoints[0] as! CGPoint)

        for i in 1 ..< linePoints.count {
            print( linePoints[i])
            path.addLine(to: linePoints[i] as! CGPoint)
        }
        lineNode.path = path.cgPath
    }

}