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

given

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.

#### Also check out my new book here 👇:

Rough Road - a zero to master software engineer guide book

You know you want to be a topnotch software engineer, but you know it's never easy to get there. From time to time, you feel lost and don't know what to do. This is what you are looking for, a guide from ground up to top level based on twenty years intensive programming experience.

Time limited offer expires on Sep 30 2019, original price $11.99, now only$4.99 via the link below:
{"extraction_info": {"found_math": true, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 1, "mathjax_display_tex": 0, "mathjax_asciimath": 1, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0, "katex": 0, "math-container": 0, "wp-katex-eq": 0, "align": 0, "equation": 0, "x-ck12": 0, "texerror": 0, "math_score": 0.46813464164733887, "perplexity": 1237.9993973868327}, "config": {"markdown_headings": true, "markdown_code": true, "boilerplate_config": {"ratio_threshold": 0.18, "absolute_threshold": 10, "end_threshold": 15, "enable": true}, "remove_buttons": true, "remove_image_figures": true, "remove_link_clusters": true, "table_config": {"min_rows": 2, "min_cols": 3, "format": "plain"}, "remove_chinese": true, "remove_edit_buttons": true, "extract_latex": true}, "warc_path": "s3://commoncrawl/crawl-data/CC-MAIN-2021-43/segments/1634323585353.52/warc/CC-MAIN-20211020214358-20211021004358-00285.warc.gz"}