iPhoneパクパク文字

文字をパクパクして並び替えるiPhoneアプリのサンプルコードを描いてみます。

import UIKit

import SpriteKit

class ViewController: UIViewController {

    weak var scene : SKScene?

    var wordstack = [SKLabelNode]()

    

    override func viewDidLoad() {

        super.viewDidLoad()

        setupScene()

        createWords()

        createCat()

        createEater()

    }

    

    func setupScene() {

        let sv = SKView(frame: view.bounds)

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

        s.backgroundColor = UIColor(hue: 0.2, saturation: 0.1, brightness: 0, alpha: 0)

        sv.presentScene(s)

        view.addSubview(sv)

        

        scene = s

    }

    

    func createWords() {

        let w = CGRectGetMaxX(view.bounds)

        let h = CGRectGetMidY(view.bounds)

        

        let words = [“A”,“C”,“T”];

        for (i, c) in enumerate(words) {

            let x = (w / 4.0) * CGFloat(i + 1)

            let label = SKLabelNode(text: c)

            label.name = “word”

            label.fontSize = 60

            label.fontColor = UIColor.whiteColor()

            label.position = CGPoint(x: x, y: h)

            scene?.addChild(label)

        }

    }

    

    func createCat() {

        let cat = SKNode()

        cat.position = CGPoint(x: CGRectGetMidX(view.bounds), y: 100)

        scene?.addChild(cat)

        

        let catsetting = {(n : SKShapeNode) -> Void in

            n.fillColor = UIColor.darkGrayColor()

            n.lineWidth = 0

            cat.addChild(n)

        }

        

        let face = SKShapeNode(circleOfRadius: 45)

        catsetting(face)

        

        let triangle = UIBezierPath()

        triangle.moveToPoint(CGPoint(x: –20, y: 0))

        triangle.addLineToPoint(CGPoint(x: 20, y: 0))

        triangle.addLineToPoint(CGPoint(x: 0, y: 40))

        

        for i in 0..<2 {

            let ear = SKShapeNode(path: triangle.CGPath, centered: true)

            ear.position = CGPoint(x: 74 * CGFloat(i) 37, y: 37)

            ear.zRotation = CGFloat(M_PI / 180.0) * 30.0 CGFloat(M_PI / 180.0) * 60.0 * CGFloat(i)

            catsetting(ear)

        }

    }

    

    func createEater() {

        let path = UIBezierPath()

        let r = 50 as CGFloat

        path.moveToPoint(CGPointZero)

        path.addLineToPoint(CGPoint(x: r, y: 0))

        path.addArcWithCenter(CGPointZero, radius: r, startAngle: 0, endAngle: CGFloat(M_PI), clockwise: true)

        

        let eater = SKNode()

        eater.name = “eater”

        eater.zPosition = 1

        for i in 0..<2 {

            let half = SKShapeNode(path: path.CGPath)

            half.lineWidth = 0

            half.fillColor = UIColor(hue: 0, saturation: 0.6, brightness: 1, alpha: 1)

            half.position = CGPoint(x:50, y:CGRectGetMidY(view.bounds) + 30)

            if i == 1 { half.zRotation = CGFloat(M_PI) }

            eater.addChild(half)

            

            let angle = {$0 2.0 * $0 * CGFloat(i)}(0.5)

            half.runAction(SKAction.repeatActionForever(SKAction.sequence([SKAction.rotateByAngle(angle, duration: 0.3), SKAction.rotateByAngle(angle, duration: 0.3)])))

        }

        scene?.addChild(eater)

    }

    

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

        let eater = scene?.childNodeWithName(“eater”)

        let words = scene?.children.filter { ($0 as SKNode).name == “word” }

            .sorted { $0.position.x < $1.position.y }

        

        if let word = words?.first as? SKLabelNode {

            eater?.runAction(SKAction.moveToX(word.position.x 50, duration: 1.0), completion: {

                self.wordstack.append(word)

                word.removeFromParent()

            })

        } else {

            if wordstack.count == 3 {

                let answer = [“C”, “A”, “T”]

                let index = {find(answer, ($0 as SKLabelNode).text)}

                wordstack.sort {index($0) < index($1)}

            }

            

            let dx = (CGRectGetMaxX(view.bounds) / 4.0)

            let word = wordstack.removeLast()

            word.name = “sorted word”

            word.position = CGPoint(x: dx * CGFloat(wordstack.count + 1), y: word.position.y)

            scene?.addChild(word)

            

            eater?.runAction(SKAction.moveToX(dx * CGFloat(wordstack.count) 50, duration: 1.5))

        }

    }

}