Last active
March 25, 2016 15:01
-
-
Save jongwonwoo/8085d262440d816db023 to your computer and use it in GitHub Desktop.
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
class ElasticMotionStateMachine { | |
func keepMoving(currentPoint: CGPoint) { | |
if self.state == .Closed || self.state == .WillClose || self.state == .Opened || self.state == .WillOpen { | |
self.beginPoint = currentPoint | |
self.totalMovingPoint = CGPointZero | |
} | |
let delta = self.deltaPointFromCurrentPoint(currentPoint) | |
self.addMovingPoint(delta) | |
self.changeState() | |
} | |
private func changeState() { | |
switch self.state { | |
case .Closed, .Opened: | |
self.moveNextState() | |
case .MayOpen, .MayClose: | |
if self.isOverThreshold() { | |
self.moveNextState() | |
let time = dispatch_time(DISPATCH_TIME_NOW, Int64(self.vibrationSec * Double(NSEC_PER_SEC))) | |
dispatch_after(time, dispatch_get_main_queue(), moveNextState) | |
} else { | |
self.stayCurrentState() | |
} | |
default: | |
break | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment