Search code examples
swift4clockmillisecondsseconds

How do I make a clock second hand move with 4 jumps/ticks per second?


EDIT: Needed help to get exactly 3 ticks per second, but that's not possible because of fractions. Double checking the real life clock I realized it actually moves 4 ticks per second, I'm thinking a quarter second is even and should be able to be exact. Updated the question since I can't get that to work either:

I'm building a clock that has three options of how the second hand will move. I've successfully made the seconds move 6° per second (1 "tick" per second) and one option with a seamless sweep (0.006° per millisecond). But I can't get my formula to work for my third option: having the second hand move exactly 4 times per second, i.e. 1.5° every quarter of a second).

This is the line of code for the second hand (I use CGAffiateTransform later, hence the radians):

let quarterSecond = round(millisecond * 4.0 / 1000.0)

let tickingSeconds = (((1.5 * π / 180) * quarterSecond) + ((6.0 * π / 180) * second))

This does what I want but I don't like the round as it makes the movement inexact. The whole timer interval is set to 0.001 and I have a hard time believing that you can't make the second hand move precisely 4 "ticks" per second without doing some uneven jerks every now and then.

Any ideas?


Solution

  • What you intuitively think as ticking is how often your expression changes value.

    Here, your entire expression is constant except for second. Which means that your expression can only change when second change. If it is an integer, it can only change every second.

    You need a variable that changes thrice a second, you can approximate that using millisecond and a truncation (to remove the fractional part):

    let thrice_a_second = (millisecond * 3.0 / 1000.0).round()

    Only then you can use that expression to compute the new position, knowing that it increases by 1 thrice a second.