Commit 277b5ef2 authored by toby's avatar toby

add block with state machine

parent add47a64
......@@ -10,11 +10,23 @@ import UIKit
enum BackgroundState:StateMachineDataSource{
case Ready, AnimatingBackground, Paused, Finished
case Ready, AnimatingBackground, AddBlock(UIView), PausedBackground, Finished
func shouldTransitionFrom(from:BackgroundState, to: BackgroundState) -> Should {
switch (from, to){
case (.Ready, .AnimatingBackground), (.Paused, .AnimatingBackground), (.AnimatingBackground, .Paused), (.AnimatingBackground, .Finished), (.Paused, .Finished), (.Finished, .AnimatingBackground):
case (.Ready, .AnimatingBackground), (.PausedBackground, .AnimatingBackground), (.AnimatingBackground, .PausedBackground), (.AnimatingBackground, .Finished), (.PausedBackground, .Finished), (.Finished, .AnimatingBackground):
return .Continue
case(.Ready, .AddBlock(let block)):
return .Continue
case(.AnimatingBackground, .AddBlock(let block)):
return .Continue
case(.PausedBackground, .AddBlock(let block)):
return .Continue
case(.AddBlock(let block), .Ready):
return .Continue
case(.AddBlock(let block), .AnimatingBackground):
return .Continue
case(.AddBlock(let block), .PausedBackground):
return .Continue
default:
return .Abort
......@@ -31,12 +43,21 @@ extension ViewController: StateMachineDelegate {
switch (from, to) {
case (.Ready, .AnimatingBackground), (.Finished, .AnimatingBackground):
animateBackground()
case (.AnimatingBackground, .Paused):
case (.AnimatingBackground, .PausedBackground):
background.pause()
case (.Paused, .AnimatingBackground), (.Paused, .Finished):
case (.PausedBackground, .AnimatingBackground), (.PausedBackground, .Finished):
background.unpause()
case (.AnimatingBackground, .Finished):
machine.state = .AnimatingBackground
case ((let origin), .AddBlock(let block)):
view.addSubview(block)
gravity.addItem(block)
collision.addItem(block)
let tap = UITapGestureRecognizer(target: self, action: Selector("blockTouch:"))
block.addGestureRecognizer(tap)
blocks.append(block)
machine.state = origin
default:
break
}
......@@ -189,7 +210,7 @@ class ViewController: UIViewController {
machine.state = .AnimatingBackground
case UIGestureRecognizerState.Ended:
machine.state = .Paused
machine.state = .PausedBackground
default:
break
......@@ -198,16 +219,10 @@ class ViewController: UIViewController {
@IBAction func newBlock(sender: AnyObject) {
let location = sender.locationInView(view)
var block = UIView(frame: CGRect(x: location.x, y: location.y, width: 60, height: 60))
let block = UIView(frame: CGRect(x: location.x, y: location.y, width: 60, height: 60))
block.center = location
block.backgroundColor = UIColor.init(red: randomPercent(), green: randomPercent(), blue: randomPercent(), alpha: 1)
view.addSubview(block)
gravity.addItem(block)
collision.addItem(block)
let tap = UITapGestureRecognizer(target: self, action: Selector("blockTouch:"))
block.addGestureRecognizer(tap)
blocks.append(block)
machine.state = .AddBlock(block)
}
@IBAction func blockTouch(recognizer: UIGestureRecognizer) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment