Last active
August 29, 2015 14:26
-
-
Save mzsima/26dcec6f7a5dfbd89b7f to your computer and use it in GitHub Desktop.
eye color illusion
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// ViewController.swift | |
// EyeColorIllusion | |
// | |
// Created by MizushimaYusuke on 8/6/15. | |
// Copyright (c) 2015 MizushimaYusuke. All rights reserved. | |
// | |
// | |
// my blog : http://lepetit-prince.net/ios/?p=4217 | |
// | |
// 参考にしたサイト: 色の恒常性 <http://www.psy.ritsumei.ac.jp/~akitaoka/colorconstancy.html>[Accessed 6 August 2015] | |
// | |
import UIKit | |
import SpriteKit | |
class ViewController: UIViewController { | |
weak var scene : SKScene? | |
weak var selected : SKSpriteNode? | |
var colorA = UIColor.cyanColor() | |
var colorB = UIColor.redColor() | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
setupScene(); | |
createFace(); | |
createCellophane(); | |
} | |
func setupScene() { | |
let sv = SKView(frame: view.bounds) | |
let s = SKScene(size: sv.frame.size) | |
s.backgroundColor = UIColor.lightGrayColor() | |
sv.presentScene(s) | |
view.addSubview(sv) | |
scene = s | |
} | |
let eyeOuter = UIBezierPath(ovalInRect: CGRectMake(-30, -30, 60, 60)) | |
let eyeInner = UIBezierPath(arcCenter: CGPointZero, radius: 5, startAngle: 0, endAngle: CGFloat(M_PI) * 2.0, clockwise: false) | |
func createFace() { | |
let face = SKShapeNode(circleOfRadius: 120) | |
face.fillColor = UIColor(white: 0.9, alpha: 1) | |
face.lineWidth = 4 | |
face.strokeColor = UIColor.darkGrayColor() | |
face.position = CGPoint(x:CGRectGetMaxX(view.bounds) * 0.75, y:CGRectGetMidY(view.bounds)) | |
scene?.addChild(face) | |
for i in 0...1 { | |
let eye = SKShapeNode(path: eyeOuter.CGPath) | |
eye.position = CGPoint(x: i==0 ? -50 : 50, y: 10) | |
eye.strokeColor = face.strokeColor | |
eye.fillColor = colorA | |
eye.lineWidth = face.lineWidth | |
let black = SKShapeNode(path: eyeInner.CGPath) | |
black.fillColor = UIColor.darkGrayColor() | |
black.strokeColor = face.strokeColor | |
eye.addChild(black) | |
face.addChild(eye) | |
} | |
let mouth = SKSpriteNode(color: UIColor.darkGrayColor(), size: CGSize(width: 30, height: 4)) | |
mouth.position = CGPoint(x: 0, y: -50) | |
face.addChild(mouth) | |
} | |
func createCellophane() { | |
let cellophane = SKSpriteNode() | |
cellophane.name = "cellophane" | |
scene?.addChild(cellophane) | |
let partA = SKSpriteNode(color: colorB.colorWithAlphaComponent(0.5), size: CGSize(width: CGRectGetMidX(view.bounds), height: CGRectGetMaxY(view.bounds))) | |
partA.position = CGPoint(x: 0, y: 0) | |
cellophane.addChild(partA) | |
let path = UIBezierPath(CGPath: eyeOuter.CGPath) | |
path.usesEvenOddFillRule = true | |
path.appendPath(eyeInner) | |
let partEye = SKShapeNode(path: path.CGPath) | |
partEye.fillColor = colorB.colorWithAlphaComponent(0.5) | |
partEye.lineWidth = 0 | |
partEye.position = CGPoint(x: CGRectGetMidX(view.bounds) * 0.5 + 50, y: 10); | |
cellophane.addChild(partEye) | |
} | |
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) { | |
if let touch = touches.first as? UITouch { | |
let p = touch.locationInNode(scene) | |
let n = scene?.nodeAtPoint(p) | |
if n?.parent?.name == "cellophane" { | |
self.selected = n?.parent as? SKSpriteNode | |
self.selected?.position = p | |
} | |
} | |
} | |
override func touchesMoved(touches: Set<NSObject>, withEvent event: UIEvent) { | |
if let touch = touches.first as? UITouch { | |
let p = touch.locationInNode(scene) | |
self.selected?.position = p | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment