Skip to content

Commit 55a9e31

Browse files
authored
Enhancement - back transition on middle of forwarding transition (#166)
1 parent 6c7c600 commit 55a9e31

File tree

2 files changed

+36
-23
lines changed

2 files changed

+36
-23
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
2+
import QuartzCore
3+
4+
extension CALayer {
5+
6+
func activeLayer() -> CALayer {
7+
presentation() ?? self
8+
}
9+
10+
}

Sources/FluidStack/Transition/RemovingPresets/AnyRemovingTransition+contextual.swift

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ extension AnyRemovingTransition {
2929
extension AnyRemovingInteraction {
3030
public enum Contextual {
3131

32+
// dismissing semantics
3233
// TODO: naming
3334
@MainActor
3435
public static func runEnclosing(
@@ -37,22 +38,21 @@ extension AnyRemovingInteraction {
3738
destinationComponent: ContextualTransitionSourceComponentType,
3839
gestureVelocity: CGPoint?
3940
) {
41+
42+
let fromViewSnapshotMaskView = UIView()
43+
fromViewSnapshotMaskView.backgroundColor = .black
44+
fromViewSnapshotMaskView.frame = transitionContext.fromViewController.view.bounds
4045

41-
let draggingView = disclosedView
42-
43-
let maskView = UIView()
44-
maskView.backgroundColor = .black
45-
maskView.frame = transitionContext.fromViewController.view.bounds
46-
47-
let fromViewMirror = AnyMirrorViewProvider.snapshot(
46+
// the reason why making snapshot is the from view will be removed from tree
47+
let fromViewSnapshot = AnyMirrorViewProvider.snapshot(
4848
caches: true,
4949
viewProvider: { transitionContext.fromViewController.view! }
5050
).view()
5151

52-
fromViewMirror.mask = maskView
53-
fromViewMirror.alpha = 1
54-
fromViewMirror.frame = transitionContext.fromViewController.view.frame
55-
52+
fromViewSnapshot.mask = fromViewSnapshotMaskView
53+
fromViewSnapshot.alpha = 1
54+
fromViewSnapshot.frame = transitionContext.fromViewController.view.layer.activeLayer().frame
55+
5656
let entrypointMirrorView = AnyMirrorViewProvider.portal(
5757
view: destinationComponent.contentView,
5858
hidesSourceOnUsing: true
@@ -71,7 +71,7 @@ extension AnyRemovingInteraction {
7171
// layering
7272
do {
7373
reparentingView.addSubview(entrypointMirrorView)
74-
reparentingView.addSubview(fromViewMirror)
74+
reparentingView.addSubview(fromViewSnapshot)
7575
}
7676

7777
// places entrypoint mirror view in the current moving view to make cross-fade
@@ -96,7 +96,7 @@ extension AnyRemovingInteraction {
9696
transitionContext.addCompletionEventHandler { _ in
9797
reparentingView.removeFromSuperview()
9898
entrypointMirrorView.removeFromSuperview()
99-
fromViewMirror.removeFromSuperview()
99+
fromViewSnapshot.removeFromSuperview()
100100
displayingSubscription.dispose()
101101
}
102102

@@ -109,11 +109,11 @@ extension AnyRemovingInteraction {
109109
do {
110110

111111
let translation = Geometry.centerAndScale(
112-
from: fromViewMirror.frame,
112+
from: disclosedView.layer.activeLayer().frame,
113113
to: CGRect(
114114
origin: transitionContext.frameInContentView(for: destinationComponent.contentView).origin,
115115
size: Geometry.sizeThatAspectFill(
116-
aspectRatio: fromViewMirror.bounds.size,
116+
aspectRatio: fromViewSnapshot.bounds.size,
117117
minimumSize: destinationComponent.contentView.bounds.size
118118
)
119119
)
@@ -126,9 +126,12 @@ extension AnyRemovingInteraction {
126126
}
127127

128128
let targetCenter = translation.center
129+
130+
let draggingViewCenter = disclosedView.layer.activeLayer().position
131+
129132
let delta = CGPoint(
130-
x: targetCenter.x - draggingView.center.x,
131-
y: targetCenter.y - draggingView.center.y
133+
x: targetCenter.x - draggingViewCenter.x,
134+
y: targetCenter.y - draggingViewCenter.y
132135
)
133136

134137
var velocity = CGVector.init(
@@ -149,7 +152,7 @@ extension AnyRemovingInteraction {
149152
}()
150153

151154
Fluid.makePropertyAnimatorsForTranformUsingCenter(
152-
view: fromViewMirror,
155+
view: fromViewSnapshot,
153156
duration: movingDuration,
154157
position: .custom(translation.center),
155158
scale: translation.scale,
@@ -163,11 +166,11 @@ extension AnyRemovingInteraction {
163166

164167
// mask
165168
UIViewPropertyAnimator(duration: 0.6, dampingRatio: 1) {
166-
maskView.frame = transitionContext.fromViewController.view.bounds
167-
maskView.frame.size.height = destinationComponent.contentView.bounds.height / translation.scale.y
168-
maskView.layer.cornerRadius = 36
169+
fromViewSnapshotMaskView.frame = transitionContext.fromViewController.view.bounds
170+
fromViewSnapshotMaskView.frame.size.height = destinationComponent.contentView.bounds.height / translation.scale.y
171+
fromViewSnapshotMaskView.layer.cornerRadius = 36
169172
if #available(iOS 13.0, *) {
170-
maskView.layer.cornerCurve = .continuous
173+
fromViewSnapshotMaskView.layer.cornerCurve = .continuous
171174
} else {
172175
// Fallback on earlier versions
173176
}
@@ -225,7 +228,7 @@ extension AnyRemovingInteraction {
225228
// cross-fade content
226229
do {
227230
UIViewPropertyAnimator(duration: movingDuration, dampingRatio: 1) {
228-
fromViewMirror.alpha = 0
231+
fromViewSnapshot.alpha = 0
229232
entrypointMirrorView.alpha = 1
230233
}
231234
}

0 commit comments

Comments
 (0)