Created
October 12, 2017 13:56
-
-
Save mzsima/0c774b164ae20bf7f7cafb20f542246e to your computer and use it in GitHub Desktop.
missing circle
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 | |
// MissingCircle | |
// | |
// Created by MizushimaYusuke on 2017/10/12. | |
// Copyright © 2017 MizushimaYusuke. All rights reserved. | |
// | |
import UIKit | |
import SpriteKit | |
class ViewController: UIViewController { | |
weak var scene: SKScene? | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
setupScene() | |
createWall() | |
} | |
func setupScene() { | |
let sv = SKView(frame: view.bounds) | |
sv.showsPhysics = true | |
let s = SKScene(size: sv.frame.size) | |
sv.presentScene(s) | |
view.addSubview(sv) | |
scene = s | |
} | |
func createWall() { | |
let wall = SKSpriteNode(color: .clear, size: view.frame.size) | |
wall.position = view.center | |
scene?.addChild(wall) | |
wall.physicsBody = SKPhysicsBody(edgeLoopFrom: CGRect(origin: CGPoint(x: -view.center.x, y: -view.center.y), size: wall.frame.size)) | |
wall.physicsBody?.isDynamic = false | |
} | |
func createMissingCircle() -> SKNode { | |
let hue = CGFloat(arc4random_uniform(10)) * 0.1 | |
let color = UIColor(hue: hue, saturation: 0.7, brightness: 1, alpha: 1) | |
let s = CGFloat(arc4random_uniform(30)) + 30 | |
UIGraphicsBeginImageContextWithOptions(CGSize(width: s, height: s), false, 0) | |
if let ctx = UIGraphicsGetCurrentContext() { | |
color.set() | |
ctx.fillEllipse(in: CGRect(x: 0, y: 0, width: s, height: s)) | |
ctx.setBlendMode(.clear) | |
ctx.fillEllipse(in: CGRect(x: s * 0.2, y: s * 0.8, width: s, height: s)) | |
} | |
let img = UIGraphicsGetImageFromCurrentImageContext()! | |
UIGraphicsEndImageContext() | |
let texture = SKTexture(image: img) | |
let n = SKSpriteNode(texture: texture) | |
n.physicsBody = SKPhysicsBody(texture: texture, size: texture.size()) | |
self.scene?.addChild(n) | |
return n | |
} | |
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { | |
if let touch = touches.first { | |
let p = touch.location(in: scene!) | |
let n = createMissingCircle() | |
n.position = p | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
http://lepetit-prince.net/ios/?p=6739