iPhoneヒマワリ

ヒマワリを点で表示するiPhoneアプリのサンプルコードを描いてみます。

import UIKit

import SpriteKit

class ViewController: UIViewController {

    weak var scene : SKScene?

    

    override func viewDidLoad() {

        super.viewDidLoad()

        setupScene()

    }

    func setupScene() {

        let sv = SKView(frame: view.bounds)

        let s = SKScene(size: sv.frame.size)

        sv.presentScene(s)

        s.backgroundColor = UIColor(hue: 0.24, saturation: 0.4, brightness: 1, alpha: 1)

        view.addSubview(sv)

        scene = s

    }

    

    

    let r = {(n : Int) -> Float in  6.0 * sqrt(Float(n))}

    let w = {(n : Int) -> Float in  Float(n) * Float(M_PI/180.0) * 137.5}

    func showFlower(o : CGPoint) {

        [Int](0120).each { n in

            let s = SKShapeNode(circleOfRadius: 5)

            let x = CGFloat(self.r(n) * cos(self.w(n))) + o.x

            let y = CGFloat(self.r(n) * sin(self.w(n))) + o.y

            s.position = CGPoint(x: x, y: y)

            s.fillColor = UIColor(hue: 0.05, saturation: 0.7, brightness: 1, alpha: 1)

            self.scene?.addChild(s)

            s.alpha = 0;

            s.runAction(SKAction.sequence([

                SKAction.waitForDuration(0.01 * NSTimeInterval(n)),

                SKAction.fadeInWithDuration(0.1)

                ]))

        }

    }

    

    override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {

        let p = touches.anyObject()!.locationInNode(scene)

        showFlower(p)

    }

}

extension Array {

    func each(doit:T -> Void) {for i in self { doit(i) }}

}