Problem with conventional approach: you want to hide a node by fading it out. But when you do that sometimes its already fading in, or partly faded out. You need to cancel any previous animation and then start the new animation.
Solution: use a state machine to capture the potential transitions between visible, fading in/out, and hidden.
To add to a node
var visibilityState: FaderStateMachine?
init() {
super.init()
visibilityState = FaderStateMachine(self, 0.4)
}
Then to hide:
visibilityState.enterState(Hiding)
or
visibilityState.enterState(Showing)
Arrgh! The sense of isValidNextState was completely wrong in the first issue of this gist. Its not what I read it as "am I a valid next state, given this source state" - its actually "given I'm the current state, can I transition to this next state".