41

According to the docs, the way to repeat a CABasicAnimation forever is to set its repeatCount to HUGE_VALF.

But in Swift, HUGE_VALF causes a compile error. Swift doesn't seem to know about the standard library (or wherever this constant resides).

What do I do now?

matt
  • 515,959
  • 87
  • 875
  • 1,141

3 Answers3

90

Set the repeatCount to Float.infinity. This compiles and works.

In all probability, HUGE_VALF was a legacy value in any case.

Still, it's a bit of a surprise that these numeric constant names are not seen by Swift. I did try importing <stdlib.h> in the bridging header but it didn't help.


But please see now Martin R's answer. Since the time when I posted my answer, Apple has stated their preferred answer explicitly: use .greatestFiniteMagnitude. It is almost the same thing as .infinity but not quite!

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • 2
    Good find. I believe you meant `repeatCount` in both Q&A though? – Mick MacCallum Jun 21 '14 at 17:38
  • 1
    I didn't mean that, in the sense that my code was in fact setting `repeatDuration` to `HUGE_VALF`. But I probably _should_ have meant that! I'll adjust. – matt Jun 21 '14 at 18:04
  • `HUGE_VALF` is defined in ``. But even it that file is added to the bridging header file, *this* constant is not exposed to Swift. – Martin R Jun 21 '14 at 18:15
  • @MartinR And why is that? That's what I don't understand. It's just lucky I found another approach... – matt Jun 21 '14 at 20:08
7

The current documentation for the repeatCount property of CAMediaTiming states:

Setting this property to greatestFiniteMagnitude will cause the animation to repeat forever.

And that compiles without problems:

let ba = CABasicAnimation()
ba.repeatCount = .greatestFiniteMagnitude

The reason that HUGE_VALF is not imported into Swift is that it is defined as a "non-trivial" macro in <math.h>:

#   define    HUGE_VALF    __builtin_huge_valf()
Martin R
  • 529,903
  • 94
  • 1,240
  • 1,382
0

I use the "HUGE" value. it is a build_in readonly var;

david
  • 391
  • 3
  • 11