Created
          June 20, 2016 12:16 
        
      - 
      
- 
        Save Tangdixi/5bdc4f51a5c1697361abe8d394b596bc to your computer and use it in GitHub Desktop. 
    Add complete callback to CAAnimationGroup
  
        
  
    
      This file contains hidden or 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
    
  
  
    
  | 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