Skip to content

Instantly share code, notes, and snippets.

@mzsima
Created August 22, 2016 13:10
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/8a825e2460b071fb6a72af6f93a08ad3 to your computer and use it in GitHub Desktop.
Save mzsima/8a825e2460b071fb6a72af6f93a08ad3 to your computer and use it in GitHub Desktop.
double ring timer
//
// ViewController.swift
// DoubleRing
//
// Created by MizushimaYusuke on 8/22/16.
// Copyright © 2016 MizushimaYusuke. All rights reserved.
//
import UIKit
import SpriteKit
class ViewController: UIViewController {
weak var scene: SKScene?
lazy var timerLabel: SKLabelNode = {
let l = SKLabelNode(text: "")
l.fontSize = 30
l.position = CGPoint(x: self.view.frame.midX, y: 200)
self.scene!.addChild(l)
return l
}()
override func viewDidLoad() {
super.viewDidLoad()
setupScene()
createBigCircle()
createSmallCircle()
self.timerLabel.text = "10.00"
}
func setupScene() {
let sv = SKView(frame: view.bounds)
let s = SKScene(size: sv.frame.size)
sv.presentScene(s)
view.addSubview(sv)
scene = s
}
func createBigCircle() {
let num = 120
let dw = 2.0 * M_PI / Double(num)
for i in 0...num {
let path = UIBezierPath()
path.moveToPoint(CGPoint.zero)
path.addLineToPoint(CGPoint(x: 100, y: 1))
path.addLineToPoint(CGPoint(x: 100, y: -1))
path.closePath()
let bar = SKShapeNode(path: path.CGPath)
bar.fillColor = UIColor(hue: 0.5, saturation: 0.5, brightness: 1, alpha: 1)
bar.strokeColor = bar.fillColor
bar.position = CGPoint(x: view.frame.midX, y: view.frame.midY)
bar.zRotation = CGFloat(i) * CGFloat(dw)
scene?.addChild(bar)
}
}
func createSmallCircle() {
UIGraphicsBeginImageContextWithOptions(CGSize(width: 150, height: 150), false, 0)
UIColor(hue: 0.5, saturation: 0.8, brightness: 0.7, alpha: 0.7).set()
CGContextFillEllipseInRect(UIGraphicsGetCurrentContext(), CGRect(x: 0, y: 0, width: 150, height: 150))
let img = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
let texture = SKTexture(image: img)
let blueCircle = SKSpriteNode(texture: texture)
blueCircle.name = "blue circle"
blueCircle.anchorPoint = CGPoint(x: 1.0 / 3.0, y: 0.5)
blueCircle.position = CGPointMake(view.frame.midX, view.frame.midY)
scene?.addChild(blueCircle)
let hole = SKShapeNode(circleOfRadius: 50)
hole.fillColor = scene!.backgroundColor
hole.lineWidth = 0
blueCircle.addChild(hole)
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
if let circle = scene?.childNodeWithName("blue circle") {
circle.runAction(
SKAction.group([
SKAction.rotateByAngle(-2.0 * CGFloat(M_PI), duration: 10.0),
SKAction.customActionWithDuration(10.0, actionBlock: { (n, t) in
let time = String(format: "%.2f", 10.0 - t)
self.timerLabel.text = time
})
]))
}
}
}
@mzsima
Copy link
Author

mzsima commented Aug 22, 2016

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