Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Xcode 16 has broken the way we calculate the distance #1380

Merged
merged 2 commits into from
Sep 19, 2024

Conversation

pbodsk
Copy link
Collaborator

@pbodsk pbodsk commented Sep 19, 2024

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 subtract displayLink.timestamp which seems to give us equally values to work with. Here is a piece from the documentation for CADisplayLink:

You calculate the expected amount of time your app has to render each frame by using targetTimestamp-timestamp. Use targetTimestamp-CACurrentMediaTime() to calculate the actual amount of time.

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

Before After
https://github.com/user-attachments/assets/37570e5b-c85d-457e-abb2-7f13b438c31b https://github.com/user-attachments/assets/30f45ad9-90b2-4b8a-af09-0448405c69ad

@pbodsk pbodsk self-assigned this Sep 19, 2024
@pbodsk pbodsk requested a review from osanoj September 19, 2024 13:05
Copy link
Contributor

@osanoj osanoj left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work 👏

with SPM section
@pbodsk pbodsk merged commit 38f90f0 into master Sep 19, 2024
1 check passed
@pbodsk pbodsk deleted the bugfix/iOS18-bugfix branch September 19, 2024 13:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants