Open jverkoey opened 7 years ago
Method-based
Pros:
Cons:
class Runtime {
func addPlan(_ plan: Plan, to target: AnyObject, onIdle: () -> Void)
}
runtime.addPlan(tween, to: layer) {
// Invoked on idle
}
runtime.addPlans([tween], to: layer) {
// Invoked on idle
}
For Idea 2 I could argue for the callback being called on any state change rather than just on idle.
CATransaction-esque
Pros:
Cons:
let group = runtime.beginActivityGroup()
group.onActiveStateChange = {
// Do something
}
runtime.addPlan(...)
group.endActivityGroup()
Rough plan of work ahead:
Example token generator API:
let token = tokenGenerator.generate(forPlan: plan)
token.activate()
token.deactivate()
Core animation implementation:
guard let token = tokenGenerator.generate(forPlan: tween) else { return }
token.activate()
CATransaction.begin()
CATransaction.setCompletionBlock {
token.deactivate()
}
target.add(animation, forKey: nil)
CATransaction.commit()
Gesture implementation:
var gestureTokens: [UIGestureRecognizer: [IsActiveTokenable]] = [:]
func addPlan(_ plan: Plan) {
let plan = plan as! Gesturable
....
gestureTokens[gestureRecognizer].append(tokenGenerator.generate(forPlan: plan))
}
func handle(gesture: UIGestureRecognizer) {
if gesture.state == .began && gestureTokens[gesture] == nil {
gestureTokens[gesture].forEach { $0.activate() }
} else if gesture.state == .ended || gesture.state == .cancelled {
gestureTokens[gesture].forEach { $0.deactivate() }
}
}
Idea 1 for API
Blocked-based
Pros:
Cons: