Skip to content

Instantly share code, notes, and snippets.

@mzsima
Created September 20, 2015 14:09
Show Gist options
  • Save mzsima/c5c8f67c6619c39dedf8 to your computer and use it in GitHub Desktop.
Save mzsima/c5c8f67c6619c39dedf8 to your computer and use it in GitHub Desktop.
number line 20
//
// ViewController.swift
// NumberLine
//
// Created by MizushimaYusuke on 9/20/15.
// Copyright © 2015 MizushimaYusuke. All rights reserved.
//
import UIKit
class ViewController: UIViewController {
weak var questBox : UIView?
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = UIColor.blackColor()
createLineNumber()
createQBox()
createButtons()
}
func createLineNumber() {
let v = UIView(frame: CGRect(x: 30, y: 200, width: CGRectGetMaxX(view.bounds) - 59, height: 5))
v.backgroundColor = UIColor.whiteColor()
view.addSubview(v)
for i in 0...20 {
let g = UIView(frame: CGRect(origin: pointOfLine(i), size: CGSize(width: 1, height: 10)))
g.backgroundColor = UIColor.whiteColor()
view.addSubview(g)
let n = UILabel()
n.text = "\(i)"
n.font = UIFont.systemFontOfSize(15)
n.textColor = UIColor.whiteColor()
n.sizeToFit()
n.center = CGPoint(x: g.center.x, y: g.center.y + 20)
view.addSubview(n)
}
}
func createQBox() {
let qb = UIView(frame: CGRect(x: 0, y: 0, width: 50, height: 50))
qb.backgroundColor = UIColor.clearColor()
qb.layer.borderColor = UIColor.whiteColor().CGColor
qb.layer.borderWidth = 2
let bar = CALayer()
bar.frame = CGRect(x: 0, y: 0, width: 2, height: 70)
bar.backgroundColor = UIColor.whiteColor().CGColor
qb.layer.addSublayer(bar)
qb.layer.anchorPoint = CGPoint(x: 0, y: 1.5)
qb.center = pointOfLine(0)
view.addSubview(qb)
let number = arc4random() % 20
let numLabel = UILabel()
numLabel.text = "\(number)"
numLabel.textColor = UIColor.whiteColor()
numLabel.font = UIFont.systemFontOfSize(30)
numLabel.sizeToFit()
numLabel.center = CGPoint(x: 25, y: 25)
qb.addSubview(numLabel)
qb.tag = Int(number)
questBox = qb
var move = (arc4random() % 20)
move = (number + move) % 20
questBox?.center = pointOfLine(Int(move))
}
func createButtons() {
for i in 1...20 {
let plus = UIButton(type: .System)
plus.backgroundColor = UIColor(hue: 0.6, saturation: 0.6, brightness: 0.8, alpha: 1)
plus.setTitle("+\(i)", forState: .Normal)
plus.center = CGPoint(x: i < 11 ? i * 60 : (i % 11 + 1) * 60, y: i < 11 ? 20 : 60)
plus.layer.cornerRadius = 5
plus.setTitleColor(UIColor.whiteColor(), forState: .Normal)
plus.sizeToFit()
view.addSubview(plus)
let mins = UIButton(type: .System)
mins.backgroundColor = UIColor(hue: 0, saturation: 0.6, brightness: 0.8, alpha: 1)
mins.setTitle("-\(i)", forState: .Normal)
mins.center = CGPoint(x: i < 11 ? i * 60 : (i % 11 + 1) * 60, y: i < 11 ? 280 : 320)
mins.layer.cornerRadius = 5
mins.setTitleColor(UIColor.whiteColor(), forState: .Normal)
mins.sizeToFit()
view.addSubview(mins)
plus.addTarget(self, action: "moveLine:", forControlEvents: .TouchUpInside)
mins.addTarget(self, action: "moveLine:", forControlEvents: .TouchUpInside)
}
}
func moveLine(b:UIButton) {
if let titleText = b.titleLabel?.text {
var n = Int(titleText[titleText.startIndex.advancedBy(1)..<titleText.endIndex])!
if titleText.hasPrefix("-") { n *= -1 }
let d : CGFloat = (CGRectGetMaxX(view.bounds) - 60) / 20.0
let dx = CGFloat(n) * d
if questBox!.center.x + dx < 30 || questBox!.center.x + dx > CGRectGetMaxX(view.bounds) - 30 {
return
}
UIView.animateWithDuration(2, animations: { () -> Void in
questBox?.center = CGPoint(x: questBox!.center.x + dx, y: questBox!.center.y)
}, completion: { (Bool) in
if fabs(self.questBox!.center.x - self.pointOfLine(self.questBox!.tag).x) < 0.1 {
self.questBox?.alpha = 0.3
self.createQBox()
}
})
}
}
func pointOfLine(i : Int) -> CGPoint {
let d : CGFloat = (CGRectGetMaxX(view.bounds) - 60) / 20.0
return CGPoint(x: d * CGFloat(i) + 30, y: 205)
}
}
@mzsima
Copy link
Author

mzsima commented Sep 20, 2015

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