Created
September 22, 2015 14:46
-
-
Save mzsima/fda98979f155ad2b2f1b to your computer and use it in GitHub Desktop.
shape name
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 | |
// ShapeName | |
// | |
// Created by Mizushima Yusuke on 9/22/15. | |
// Copyright © 2015 Yusuke Mizusima. All rights reserved. | |
// | |
import UIKit | |
class ViewController: UIViewController { | |
enum Shape : String { | |
case Rectangle = "しかく", Triangle = "さんかく", Circle = "まる" | |
} | |
let positions = [CGPoint(x:120, y:140), CGPoint(x:250, y:250), CGPoint(x: 100, y: 350)] | |
var answer : Shape? | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
view.backgroundColor = UIColor.brownColor() | |
createGrass() | |
createStartButton() | |
} | |
func createGrass() { | |
let makeGrass = {() -> UIView in | |
let n = UIView() | |
for i in 0...10 { | |
let g = UIView(frame: CGRect(x: 0, y: 0, width: 10, height: 50)) | |
g.layer.anchorPoint = CGPoint(x: 0.5, y: 1) | |
g.layer.position = CGPoint(x: i * 8, y: 0) | |
let bright = CGFloat(arc4random() % 5) * 0.1 + 0.5 | |
g.layer.backgroundColor = UIColor(hue: 0.25, saturation: 0.8, brightness: bright, alpha: 0.9).CGColor | |
g.layer.transform = CATransform3DMakeRotation(CGFloat(M_PI) * 0.02 * CGFloat(i) - CGFloat(M_PI * 0.1), 0, 0, 1) | |
n.addSubview(g) | |
} | |
n.userInteractionEnabled = false | |
n.layer.zPosition = 100 | |
return n | |
} | |
for p in positions { | |
let grass = makeGrass() | |
grass.center = p | |
view.addSubview(grass) | |
} | |
} | |
func createStartButton() { | |
let btn = UIButton(type: .System) | |
btn.setTitle("START", forState: .Normal) | |
btn.titleLabel?.font = UIFont.boldSystemFontOfSize(30) | |
btn.setTitleColor(UIColor.whiteColor(), forState: .Normal) | |
btn.sizeToFit() | |
btn.center = CGPoint(x: CGRectGetMaxX(view.bounds) - 100, y: CGRectGetMaxY(view.bounds) - 100) | |
view.addSubview(btn) | |
btn.addTarget(self, action: "start", forControlEvents: .TouchUpInside) | |
} | |
func start() { | |
clear() | |
let shapes : [Shape] = [.Rectangle, .Triangle, .Circle] | |
answer = shapes[Int(arc4random() % 3)] | |
let targetShape = shape(answer!, color: UIColor.whiteColor()) | |
targetShape.tag = 1 | |
targetShape.center = CGPoint(x: CGRectGetMidX(view.bounds), y: 480) | |
targetShape.transform = CGAffineTransformMakeScale(0, 0) | |
view.addSubview(targetShape) | |
UIView.animateWithDuration(0.3) { () -> Void in | |
targetShape.transform = CGAffineTransformIdentity | |
} | |
for i in 0...2 { | |
let tx = UILabel() | |
tx.userInteractionEnabled = true | |
tx.text = shapes[i].rawValue | |
tx.tag = 2 | |
tx.backgroundColor = UIColor.clearColor() | |
tx.font = UIFont.boldSystemFontOfSize(30) | |
tx.textColor = UIColor.whiteColor() | |
tx.layer.anchorPoint = CGPoint(x: 0.2, y: 1.0) | |
tx.sizeToFit() | |
tx.center = positions[i] | |
tx.transform = CGAffineTransformMakeTranslation(0, -800) | |
view.addSubview(tx) | |
UIView.animateWithDuration(0.5, animations: { () -> Void in | |
tx.transform = CGAffineTransformIdentity | |
}) | |
} | |
} | |
func clear() { | |
self.view.subviews.forEach { (v:UIView) -> () in | |
if v.tag >= 1 { v.removeFromSuperview() } | |
} | |
} | |
func shape(type:Shape, color:UIColor) -> UIView { | |
let rectView = UIView(frame: CGRectMake(-40, -40, 80, 80)) | |
rectView.backgroundColor = color | |
let tri = UIView() | |
let path = UIBezierPath() | |
path.moveToPoint(CGPoint(x: -40, y: 40)) | |
path.addLineToPoint(CGPoint(x: 0, y: -40)) | |
path.addLineToPoint(CGPoint(x: 40, y: 40)) | |
path.closePath() | |
let l = CAShapeLayer() | |
l.path = path.CGPath | |
l.fillColor = color.CGColor | |
tri.layer.addSublayer(l) | |
let circle = UIView(frame: CGRectMake(-40, -40, 80, 80)) | |
circle.layer.cornerRadius = 40 | |
circle.backgroundColor = color | |
switch type { | |
case .Rectangle: return rectView | |
case .Triangle: return tri | |
case .Circle: return circle | |
} | |
} | |
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) { | |
if let touch = touches.first { | |
let p = touch.locationInView(view) | |
if let hit = view.hitTest(p, withEvent: nil) { | |
if hit.tag == 2 { | |
UIView.animateWithDuration(0.3, animations: { () -> Void in | |
hit.transform = CGAffineTransformMakeTranslation(0, -50) | |
}, completion: { (Bool) -> Void in | |
UIView.animateWithDuration(0.7, delay: 0.8, options: .CurveEaseInOut, animations: { () -> Void in | |
hit.transform = CGAffineTransformIdentity | |
}, completion: {(Bool) -> Void in | |
if (hit as! UILabel).text == self.answer!.rawValue { | |
self.start() | |
} | |
}) | |
}) | |
} | |
} | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
http://lepetit-prince.net/ios/?p=4380