0

I am creating my own custom activity indicator. I created a circular view with CAShapeLayer and I managed to stroke the circular layer but I want to do it indefinitely until the user wants to stop. The following is my stroke layer animation code.

private func getStrokeEndAnimation()->CABasicAnimation{
        let animation = CABasicAnimation(keyPath: "strokeEnd")
        animation.fromValue = 0.0
        animation.toValue = 1.0
        animation.duration = 2.0
        animation.fillMode = .forwards
        animation.isRemovedOnCompletion = false
        return animation
}

There is an instance property on BasicAnimation named repeatCount but if I specify that my activity indicator will animate the given number of times like if I do

animation.repeatCount = 3

it will animate only 3 times. How can I make sure that the animation keeps going indefinitely until, I stop it.

Natasha
  • 6,651
  • 3
  • 36
  • 58

2 Answers2

1

animation.repeatCount = .greatestFiniteMagnitude will for all practical purposes repeat it forever.

jrturton
  • 118,105
  • 32
  • 252
  • 268
  • Thank you, don't know how I missed it. However just for curiosity when should I use .greatestFiniteMagnitude and when should I use .infinity – Natasha Aug 06 '20 at 12:26
  • please read out apple docs ...it said `This value compares greater than or equal to all finite numbers, but less than infinity. This value corresponds to type-specific C macros such as FLT_MAX and DBL_MAX. The naming of those macros is slightly misleading, because infinity is greater than this value.` – Jawad Ali Aug 06 '20 at 12:30
  • here is the link https://developer.apple.com/documentation/swift/double/1849607-greatestfinitemagnitude – Jawad Ali Aug 06 '20 at 12:31
  • so using .infinity is not misleading :) ... it actually simply shows what you meant – Jawad Ali Aug 06 '20 at 12:32
  • 1
    To be honest it never occurred to me to use .infinity till seeing the other answers and the duplicate, every day is a school day! – jrturton Aug 06 '20 at 14:40
1

You can use infinity in repeat count according to Apple docs

Infinity compares greater than all finite numbers and equal to other infinite values.

animation.repeatCount = .infinity
Jawad Ali
  • 13,556
  • 3
  • 32
  • 49