@@ -49,6 +49,10 @@ final class BackTransition {
4949 /// taking one when `will-navigate` arrives is too late: the page waits 120 ms at most, and a
5050 /// snapshot on the simulator took longer, so it showed the new page (run 46165013120).
5151 private var latest : UIImage ?
52+ /// When `latest` was asked for, and when the page last reported a route: a picture asked for
53+ /// before the last route is of another page and is not used.
54+ private var latestAskedAt = Date . distantPast
55+ private var lastRouteAt = Date . distantPast
5256 private var refreshWork : DispatchWorkItem ?
5357
5458 func refresh ( after seconds : TimeInterval ) {
@@ -57,8 +61,11 @@ final class BackTransition {
5761 guard let self = self , let webView = self . webView , self . under == nil , self . cover == nil else { return }
5862 let config = WKSnapshotConfiguration ( )
5963 config . afterScreenUpdates = false
64+ let asked = Date ( )
6065 webView . takeSnapshot ( with : config ) { [ weak self ] image , _ in
61- if let image = image { self ? . latest = image }
66+ guard let self = self , let image = image , asked >= self . lastRouteAt else { return }
67+ self . latest = image
68+ self . latestAskedAt = asked
6269 }
6370 }
6471 refreshWork = item
@@ -69,14 +76,16 @@ final class BackTransition {
6976 /// at once, so the page can go ahead.
7077 func willPush ( tab : String , completion : @escaping ( ) -> Void ) {
7178 refreshWork ? . cancel ( )
72- stacks [ tab , default : [ ] ] . append ( latest )
79+ // No fresh picture of this page: no slide for it later (better than the wrong page).
80+ stacks [ tab , default : [ ] ] . append ( latestAskedAt >= lastRouteAt ? latest : nil )
7381 latest = nil
7482 completion ( )
7583 }
7684
7785 /// A `route` with `depth`: drop or pad snapshots so this tab holds depth - 1 entries.
7886 func route ( tab : String , depth : Int ? ) {
7987 currentTab = tab
88+ lastRouteAt = Date ( )
8089 if let depth = depth {
8190 var stack = stacks [ tab ] ?? [ ]
8291 let wanted = max ( 0 , depth - 1 )
@@ -96,7 +105,7 @@ final class BackTransition {
96105 func begin ( ) {
97106 guard let webView = webView , let container = webView . superview , under == nil ,
98107 let image = stacks [ currentTab ] ? . last ?? nil else { return }
99- let view = UIView ( frame : webView . frame )
108+ let view = UIView ( frame : restingFrame ( webView ) )
100109 view . clipsToBounds = true
101110 let picture = UIImageView ( image : image )
102111 picture . frame = view . bounds
@@ -154,11 +163,17 @@ final class BackTransition {
154163 end ( through : true , back : back )
155164 }
156165
166+ /// The web view's frame without the slide's transform.
167+ private func restingFrame ( _ view : UIView ) -> CGRect {
168+ CGRect ( x : view . center . x - view . bounds . width / 2 , y : view . center . y - view . bounds . height / 2 ,
169+ width : view . bounds . width , height : view . bounds . height )
170+ }
171+
157172 private func showCover ( ) {
158173 guard let webView = webView , let container = webView . superview ,
159174 let image = stacks [ currentTab ] ? . last ?? nil else { return }
160175 let view = UIImageView ( image : image )
161- view . frame = webView . frame
176+ view . frame = restingFrame ( webView )
162177 view . contentMode = . scaleToFill
163178 container . insertSubview ( view , aboveSubview : webView )
164179 cover ? . removeFromSuperview ( )
0 commit comments