Skip to content

Commit

Permalink
⭐️ Impl: Add CAAnimation+Block
Browse files Browse the repository at this point in the history
  • Loading branch information
dominicstop committed May 1, 2023
1 parent 8d94f81 commit 596cce5
Showing 1 changed file with 61 additions and 0 deletions.
61 changes: 61 additions & 0 deletions ios/src_library/Extensions/CAAnimation+Block.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
//
// CAAnimation+Block.swift
// react-native-ios-modal
//
// Created by Dominic Go on 5/1/23.
//

import Foundation


public class CAAnimationBlockDelegate: NSObject, CAAnimationDelegate {

public typealias StartBlock = (CAAnimation) -> ();
public typealias EndBlock = (CAAnimation, Bool) -> ();

var onStartBlock: StartBlock?
var onEndBlock: EndBlock?

public func animationDidStart(_ anim: CAAnimation) {
self.onStartBlock?(anim);
};

public func animationDidStop(_ anim: CAAnimation, finished flag: Bool) {
self.onEndBlock?(anim, flag);
};
};

public extension CAAnimation {

private var multicastDelegate: CAAnimationMulticastDelegate {
guard let delegate = self.delegate else {
return CAAnimationMulticastDelegate();
};

guard let multicastDelegate = delegate as? CAAnimationMulticastDelegate else {
let multicastDelegate = CAAnimationMulticastDelegate();
multicastDelegate.emitter.add(delegate);

self.speed = 0;

self.delegate = multicastDelegate;
return multicastDelegate;
};

return multicastDelegate;
};

func startBlock(_ callback: @escaping CAAnimationBlockDelegate.StartBlock) {
let blockDelegate = CAAnimationBlockDelegate();
self.multicastDelegate.emitter.add(blockDelegate);

blockDelegate.onStartBlock = callback;
};

func endBlock(_ callback: @escaping CAAnimationBlockDelegate.EndBlock) {
let blockDelegate = CAAnimationBlockDelegate();
self.multicastDelegate.emitter.add(blockDelegate);

blockDelegate.onEndBlock = callback;
};
};

0 comments on commit 596cce5

Please sign in to comment.