1616import UIKit
1717
1818/**
19- * An object that animates when the Row is inserted and deleted from the AloeStackView.
19+ * An object that animates when the Row is inserted and removed from the AloeStackView.
2020 */
21- public class AnimationCoordinator {
21+ internal class AnimationCoordinator {
2222
2323 // MARK: Internal
2424
@@ -30,34 +30,52 @@ public class AnimationCoordinator {
3030 }
3131
3232 internal func startAnimation( ) {
33- ( target. contentView as? CustomAnimating ) ? . willAnimate ( with: self )
34- UIView . animate ( withDuration: AnimationCoordinator . defaultAnimationDuration, animations: {
33+ let config = target. contentView as? CustomAnimating
34+
35+ willBeginAnimation ? ( )
36+
37+ UIView . animate ( withDuration: AnimationCoordinator . defaultAnimationDuration,
38+ delay: 0 ,
39+ usingSpringWithDamping: config? . springDamping ?? 1 ,
40+ initialSpringVelocity: 0 ,
41+ options: [ . curveEaseInOut, . allowUserInteraction, . beginFromCurrentState] ,
42+ animations: {
43+ self . animate ? ( )
3544 self . animations ( )
36- self . alongsideAnimation ? ( )
37- } ) { success in
38- self . completion ? ( success)
39- self . alongsideCompletion ? ( success)
45+ } ) { finished in
46+ self . animationDidEnd ? ( finished)
47+ self . completion ? ( finished)
4048 }
4149 }
4250
43- // MARK: Public
44-
45- public let state : State
46-
47- /// AloeStackView gives you the opportunity to change state values by taking over the animation behavior when Row is added and removed.
48- public func animate( alongsideAnimation animation: ( ( ) -> Void ) ? , completion: ( ( Bool ) -> Void ) ? = nil ) {
49- alongsideAnimation = animation
50- alongsideCompletion = completion
51- }
5251
5352 // MARK: Private
5453
5554 private let target : StackViewCell
55+ private let state : State
5656 private let animations : ( ) -> Void
5757 private let completion : ( ( Bool ) -> Void ) ?
5858
59- private var alongsideAnimation : ( ( ) -> Void ) ?
60- private var alongsideCompletion : ( ( Bool ) -> Void ) ?
59+ private lazy var willBeginAnimation : ( ( ) -> Void ) ? = {
60+ switch state {
61+ case . insert: return ( target. contentView as? CustomAnimating ) ? . insertAnimationWillBegin
62+ case . remove: return ( target. contentView as? CustomAnimating ) ? . removeAnimationWillBegin
63+ }
64+ } ( )
65+
66+ private lazy var animate : ( ( ) -> Void ) ? = {
67+ switch state {
68+ case . insert: return ( target. contentView as? CustomAnimating ) ? . animateInsert
69+ case . remove: return ( target. contentView as? CustomAnimating ) ? . animateRemove
70+ }
71+ } ( )
72+
73+ private lazy var animationDidEnd : ( ( Bool ) -> Void ) ? = {
74+ switch state {
75+ case . insert: return ( target. contentView as? CustomAnimating ) ? . insertAnimationDidEnd
76+ case . remove: return ( target. contentView as? CustomAnimating ) ? . removeAnimationDidEnd
77+ }
78+ } ( )
6179
6280 private static let defaultAnimationDuration : TimeInterval = 0.3
6381
0 commit comments