Skip to content

Instantly share code, notes, and snippets.

@Tangdixi
Created June 20, 2016 12:16
Show Gist options
  • Save Tangdixi/5bdc4f51a5c1697361abe8d394b596bc to your computer and use it in GitHub Desktop.
Save Tangdixi/5bdc4f51a5c1697361abe8d394b596bc to your computer and use it in GitHub Desktop.
Add complete callback to CAAnimationGroup
import UIKit
typealias CAAnimationGroupCompletion = (()->Void)
class CAAnimationGroupCompletionWrapper {
var closure:(CAAnimationGroupCompletion)?
init(closure:CAAnimationGroupCompletion?) {
self.closure = closure
}
}
extension CAAnimationGroup {
private struct AssociatedKeys {
static var descriptiveName = "CAAnimationGroupClosure"
}
var completion:(()->Void)? {
set {
if let newValue = newValue {
self.delegate = self
objc_setAssociatedObject(
self,
&AssociatedKeys.descriptiveName,
CAAnimationGroupCompletionWrapper(closure: newValue),
.OBJC_ASSOCIATION_RETAIN_NONATOMIC)
}
}
get {
guard let closureWrapper = objc_getAssociatedObject(self, &AssociatedKeys.descriptiveName) as? CAAnimationGroupCompletionWrapper else { return nil }
return closureWrapper.closure
}
}
public override func animationDidStop(anim: CAAnimation, finished flag: Bool) {
if flag == true {
guard let closureWrapper = objc_getAssociatedObject(self, &AssociatedKeys.descriptiveName) as? CAAnimationGroupCompletionWrapper else { return }
guard let closure = closureWrapper.closure else { return }
closure()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment