0

The Details:

  • I am using DispatchQueue.main.asyncAfter(deadline: ... to update the snapPoint of a UISnapBehavior, which causes the view associated with the snap behavior to snap (with animation) to the new snap point.
  • I want to update the snapPoint of three circular views each of which are stacked one on top of the other.
  • I want to update the snapPoint of each consecutively. In other words, I want view A to start animating into position at t0, view B to start animating at t1, and view C to start animating at t2.

The Problem:

The timing of the animations is bizarre. For example:

  • If the difference between t0, t1, and t2 is 100 milliseconds, they all snap to their respective snap points at exactly the same time.
  • However, if the difference between t0, t1, and t2 is 200 milliseconds, then view A and view B snap to their respective positions at the same time, but view C snaps to its new snap point at a totally different time later than the first two.
  • Even more bizarre, if the difference between t0, t1, and t2 reach 400 milliseconds, then view A animates on its own, and views B and C animate together.
  • Finally, if the difference reaches 500 milliseconds, each view animates at a different time.

What is going on here?

enter image description here

josephap
  • 2,075
  • 17
  • 24

1 Answers1

0

Using Timer (Swift) or NSTimer (Objective-C) instead of GCD (Grand Central Dispatch) is allowing me to make these nuanced delays in animations with expected behavior.

enter image description here

However, I do not know why this works. I greatly welcome explanations as to what is happening under the hood or if it is possible to achieve the same effect with GCD.

josephap
  • 2,075
  • 17
  • 24
  • `dispatchAfter` does exactly that; dispatch *after* some time delay, the actual delay may be longer, but it won't be shorter. The actual delay depend on what he system decides to do. – Paulw11 May 02 '17 at 21:36
  • It does exactly that...theoretically. If you repeat the steps I did above with values around 100 milliseconds (0.1 seconds), everything happens at exactly the same time. If you run the same code with a timer, the animation occurs as expected. I don't know why, but that is what happens despite what one might expect. – josephap May 02 '17 at 21:51
  • It's the difference between saying "Come and see me *at* 4 o'clock" vs "Come and see me *after* 4 o'clock". In the first case, I am expected to be there right on 4. In the second case, 4:15 is OK. – Paulw11 May 02 '17 at 22:38