fix: Xcode 16 has broken the way we calculate the distance #1380
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Why?
I don't know...but they did 😉
What?
Apparently, the way we calculate how much to move for each "tick" in the
CADisplayLink
is broken.Currently
We calculate a
duration
(a delta time since last render) and use that to calculate how much (position
) and how fast (velocity
) to use.That duration is calculated like so:
let duration = displayLink.targetTimestamp - CACurrentMediaTime()
In the end of each "tick" we use this check:
if position < scale, velocity < scale
to determine if we should stop the animation or not.
But
The calculation
displayLink.targetTimestamp - CACurrentMediaTime()
has changed in Xcode 16, meaning that we can now get negative values from this calculation.That again means that our velocity and position starts to act funny and we never end up in a positive check, meaning that our animation just continues forever.
We are not the only ones seeing this problem:
https://forums.developer.apple.com/forums/thread/762291
The Fix
Instead of subtracting
CACurrentMediaTime
we subtractdisplayLink.timestamp
which seems to give us equally values to work with. Here is a piece from the documentation forCADisplayLink
:This seems to work and leaves us with similar results both in iOS 18 and pre iOS 18 so we'll go with that
Show me