Skip to content

Instantly share code, notes, and snippets.

@mzsima
Created March 12, 2016 13:27
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mzsima/23f59d4ec89b5cf13d99 to your computer and use it in GitHub Desktop.
Save mzsima/23f59d4ec89b5cf13d99 to your computer and use it in GitHub Desktop.
pentagon rose
//
// ViewController.swift
// PentagonRose
//
// Created by Mizushima Yusuke on 3/12/16.
// Copyright © 2016 Yusuke Mizusima. All rights reserved.
//
import UIKit
import SpriteKit
class ViewController: UIViewController {
weak var scene : SKScene?
var yellowArr = [SKNode]()
override func viewDidLoad() {
super.viewDidLoad()
setupScene()
createRose()
}
func setupScene() {
let sv = SKView(frame: view.bounds)
let s = SKScene(size: sv.frame.size)
sv.presentScene(s)
view.addSubview(sv)
scene = s
}
func createRose() {
self.view.backgroundColor = UIColor.greenColor()
for i in 0...5 {
let o = CGPoint(x: CGRectGetMidX(view.bounds), y: CGRectGetMidY(view.bounds))
let r = Double(7 - i) * 30 * pow(0.8, Double(i))
let red = createPentagonWithColor(r, color: .redColor())
let yellow = createPentagonWithColor(r, color: .yellowColor())
red.position = o
yellow.position = o
self.scene?.addChild(red)
self.scene?.addChild(yellow)
red.zRotation = -CGFloat(M_PI / 12.0) * CGFloat(i)
yellow.zRotation = red.zRotation
yellowArr.append(yellow)
}
}
func createPentagonWithColor(radius : Double, color : UIColor) -> SKNode {
let dw = M_PI / 2.5
let path = UIBezierPath()
for i in 0...4 {
let x = radius * cos(Double(i) * dw)
let y = radius * sin(Double(i) * dw)
if i == 0 {
path.moveToPoint(CGPoint(x: x, y: y))
} else {
path.addLineToPoint(CGPoint(x: x, y: y))
}
}
path.closePath()
let pentagon = SKShapeNode()
pentagon.path = path.CGPath
pentagon.fillColor = color
pentagon.lineWidth = 1
pentagon.strokeColor = UIColor.whiteColor()
return pentagon
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
yellowArr.enumerate().forEach {
$1.runAction(SKAction.sequence([
SKAction.waitForDuration(Double($0)),
SKAction.group([
SKAction.scaleBy(0.86, duration: 1.0),
SKAction.rotateByAngle(CGFloat(M_PI) / 12.0, duration: 1.0)
])
]))
}
}
}
@mzsima
Copy link
Author

mzsima commented Mar 12, 2016

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment