Created
August 22, 2016 13:10
-
-
Save mzsima/8a825e2460b071fb6a72af6f93a08ad3 to your computer and use it in GitHub Desktop.
double ring timer
This file contains 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 | |
// 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 | |
}) | |
])) | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
http://lepetit-prince.net/ios/?p=5433