How UIPushBehavior instantaneous mode works

Today I worked with UIKit Dynamics for implementing some physical based UI effects. It’s the first time I use it, I was really impressed how easy it is to bring awesome physical UI effects in your app. Usually physical engines only available in games, but it’s really cool that Apple brings this kind of technology to iOS environment just for building UI.

While I was using it, there is one thing that confused me. That’s UIPushBehavior’s instantaneous mode. For UIPushBehavior’s another mode continuous, it says very clearly in the document

A continuous force vector with a magnitude of 1.0, applied to a 100 point x 100 point view whose density value is 1.0, results in view acceleration of 100 points / second² in the direction of the vector; this value is also known as the UIKit Newton.

We all know by Newton’s second law of motion

F = ma

as long as we know the force, which is UIPushBehavior.magnitude in this case, and for the mass, we can calculate easily by

view.width * view.height * density

then we can get the acceleration. With the acceleration and elapsed time (time delta), we then can know how much velocity we gain from the force to the object.

Then what about instantaneous mode? It sounds like applying the force on the object instantly, but without the time delta, how can we know the what’s the velocity to be added on the object?

I then googled around, and found this post on stackoverflow. It says

1 magnitude adds around 100 points / second velocity on a 100 * 100 mass object

To prove what he said is true, I also wrote a simple program to test it out. Turns out it’s the same number. And the way it works is, the velocity will be added after the first frame processed by the UIDynamicAnimator.

With these numbers in mind, it’s not hard to understand how it works now. I thought we can see the instantaneous mode UIPushBehavior.magnitude as momentum. Given the formula

p = mv

The momentum instantaneous mode UIPushBehavior provides is

magnitude * (100 points / second) * (100 * 100 mass)

To know how much velocity the UIPushBehavior will add on the target object is easy


p = mv

so that

magnitude * (100 points / second) * (100 * 100 mass) = (view mass) * velocity

so that

velocity = (magnitude * (100 points / second) * (100 * 100 mass)) / (view mass)

For an example, the velocity for a 200 * 100 object with a 2 magnitude instantaneous UIPushBehavior, it will be

velocity = (2 * (100 points / second) * (100 * 100 mass)) / (200 * 100) velocity = 100 points / second

So that we know 100 points / second will be added to the target object immediately.

Recent articles:

How I discovered a 9.8 critical security vulnerability in ZeroMQ with mostly pure luck and my two cents about xz backdoor
High-speed 10Gbps full-mesh network based on USB4 for just $47.98
Why I built a self-serving advertisement solution for myself