Skip to content

Commit 07cfd38

Browse files
committed
add transition
1 parent e9f1d1c commit 07cfd38

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

Sources/UIComponent/Animators/TransformAnimator.swift

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ public struct TransformAnimator: Animator {
1414
public var duration: TimeInterval
1515
/// A Boolean value that determines whether the animation should be applied in a cascading manner.
1616
public var cascade: Bool
17+
/// A Boolean value that determines whether to show the initial insertion animation when the view is first loaded.
18+
public var showInitialInsertionAnimation: Bool = false
1719

1820
/// Initializes a new animator with the specified transform, duration, and cascade options.
1921
/// - Parameters:
@@ -23,15 +25,18 @@ public struct TransformAnimator: Animator {
2325
public init(
2426
transform: CATransform3D = CATransform3DIdentity,
2527
duration: TimeInterval = 0.5,
26-
cascade: Bool = false
28+
cascade: Bool = false,
29+
showInitialInsertionAnimation: Bool = false
2730
) {
2831
self.transform = transform
2932
self.duration = duration
3033
self.cascade = cascade
34+
self.showInitialInsertionAnimation = showInitialInsertionAnimation
3135
}
3236

3337
public func delete(hostingView: UIView, view: UIView, completion: @escaping () -> Void) {
3438
if hostingView.componentEngine.isReloading, hostingView.bounds.intersects(view.frame) {
39+
let baseTransform = view.layer.transform
3540
UIView.animate(
3641
withDuration: duration,
3742
delay: 0,
@@ -44,7 +49,7 @@ public struct TransformAnimator: Animator {
4449
},
4550
completion: { _ in
4651
if !hostingView.componentEngine.visibleViews.contains(view) {
47-
view.transform = CGAffineTransform.identity
52+
view.layer.transform = baseTransform
4853
view.alpha = 1
4954
}
5055
completion()
@@ -58,7 +63,8 @@ public struct TransformAnimator: Animator {
5863
public func insert(hostingView: UIView, view: UIView, frame: CGRect) {
5964
view.bounds.size = frame.size
6065
view.center = frame.center
61-
if hostingView.componentEngine.isReloading, hostingView.componentEngine.hasReloaded, hostingView.bounds.intersects(frame) {
66+
let baseTransform = view.layer.transform
67+
if hostingView.componentEngine.isReloading, showInitialInsertionAnimation || hostingView.componentEngine.hasReloaded, hostingView.bounds.intersects(frame) {
6268
let offsetTime: TimeInterval = cascade ? TimeInterval(frame.origin.distance(hostingView.bounds.origin) / 3000) : 0
6369
UIView.performWithoutAnimation {
6470
view.layer.transform = transform
@@ -71,7 +77,7 @@ public struct TransformAnimator: Animator {
7177
initialSpringVelocity: 0,
7278
options: [.allowUserInteraction],
7379
animations: {
74-
view.transform = .identity
80+
view.layer.transform = baseTransform
7581
view.alpha = 1
7682
}
7783
)
@@ -105,15 +111,14 @@ public struct TransformAnimator: Animator {
105111
completion: nil
106112
)
107113
}
108-
if view.alpha != 1 || view.transform != .identity {
114+
if view.alpha != 1 {
109115
UIView.animate(
110116
withDuration: duration,
111117
delay: 0,
112118
usingSpringWithDamping: 0.9,
113119
initialSpringVelocity: 0,
114120
options: [.allowUserInteraction],
115121
animations: {
116-
view.transform = .identity
117122
view.alpha = 1
118123
},
119124
completion: nil

0 commit comments

Comments
 (0)