Commit d0f254c1 authored by toby's avatar toby

remove block with state

parent b36d3ff4
......@@ -10,12 +10,16 @@ import UIKit
enum BackgroundState:StateMachineDataSource{
case Ready, AnimatingBackground, AddBlock(UIView), PausedBackground, Finished
case Ready, AnimatingBackground, AddBlock(UIView), RemoveBlock(UIView), PausedBackground, Finished
func shouldTransitionFrom(from:BackgroundState, to: BackgroundState) -> Should {
switch (from, to){
case (.Ready, .AnimatingBackground), (.PausedBackground, .AnimatingBackground), (.AnimatingBackground, .PausedBackground), (.AnimatingBackground, .Finished), (.PausedBackground, .Finished), (.Finished, .AnimatingBackground):
return .Continue
case(_, .RemoveBlock(let block)):
return .Continue
case(.RemoveBlock(let block), _):
return .Continue
case(.Ready, .AddBlock(let block)):
return .Continue
case(.AnimatingBackground, .AddBlock(let block)):
......@@ -49,12 +53,11 @@ extension ViewController: StateMachineDelegate {
background.unpause()
case (.AnimatingBackground, .Finished):
machine.state = .AnimatingBackground
case((let originState), .RemoveBlock(let block)):
removeBlock(block)
machine.state = originState
case ((let originState), .AddBlock(let block)):
view.addSubview(block)
gravity.addItem(block)
collision.addItem(block)
block.addGestureRecognizer(tap)
blocks.append(block)
addBlock(block)
machine.state = originState
default:
break
......@@ -75,7 +78,6 @@ class ViewController: UIViewController {
var blocks: [UIView] = [UIView]()
var barriers: [UIView] = [UIView]()
var tap: UITapGestureRecognizer!
var hold: UILongPressGestureRecognizer!
override func viewDidLoad() {
......@@ -111,7 +113,6 @@ class ViewController: UIViewController {
animator.addBehavior(collision)
// Setup gestures
tap = UITapGestureRecognizer(target: self, action: Selector("blockTouch:"))
hold = UILongPressGestureRecognizer(target: self, action: Selector("backgroundHeld:"))
view.addGestureRecognizer(hold)
......@@ -175,6 +176,15 @@ class ViewController: UIViewController {
})
}
private func addBlock(block: UIView) {
let tap = UITapGestureRecognizer(target: self, action: Selector("blockTouch:"))
view.addSubview(block)
gravity.addItem(block)
collision.addItem(block)
block.addGestureRecognizer(tap)
blocks.append(block)
}
private func removeBlock(block: UIView) {
UIView.animateWithDuration(
0.2,
......@@ -190,6 +200,7 @@ class ViewController: UIViewController {
block.removeFromSuperview()
self.gravity.removeItem(block)
self.collision.removeItem(block)
self.blocks.filter({ $0 != block })
})
}
......@@ -210,10 +221,8 @@ class ViewController: UIViewController {
switch hold.state {
case UIGestureRecognizerState.Began:
machine.state = .AnimatingBackground
case UIGestureRecognizerState.Ended:
machine.state = .PausedBackground
default:
break
}
......@@ -228,7 +237,7 @@ class ViewController: UIViewController {
}
@IBAction func blockTouch(recognizer: UIGestureRecognizer) {
removeBlock(recognizer.view!)
machine.state = .RemoveBlock(recognizer.view!)
}
}
\ No newline at end of file
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