Home > Software engineering >  Fill the CALayer with different color when timer reaches to 5 seconds swift ios
Fill the CALayer with different color when timer reaches to 5 seconds swift ios

Time:07-12

In my app i am using a third party library which does circular animation just like in the appstore app download animation. i am using the external file which i have placed in my project. it works but when the timer reaches to 5 seconds the fill color should be red. Currently the whole layer red color applies, i want to only apply the portion it has progressed. i am attaching the video and the third party file. Please can anyone help me with this as what changes should i make to the library file or is there better solution

enter image description here

UPDATE:

CAKeyframeAnimation:

  • You specify the keyframe values using the values and keyTimes properties.

For example:

let colorKeyframeAnimation = CAKeyframeAnimation(keyPath: "backgroundColor")

colorKeyframeAnimation.values = [UIColor.red.cgColor,
                                 UIColor.green.cgColor,
                                 UIColor.blue.cgColor]
colorKeyframeAnimation.keyTimes = [0, 0.5, 1]
colorKeyframeAnimation.duration = 2

This animation will run for the 2.0 duration.

  • We have 3 values and 3 keyTimes in this example.
  • 0 is the initial point and 1 be the last point.
  • It shows which associated values will reflect at particular interval [keyTimes] during the animation.

i.e:

KeyTime 0.5 -> (2 * 0.5) -> At 1 sec during animation -> UIColor.green.cgColor will be shown.

Now to answer your question from the comments, Suppose we have timer of 25 seconds and we want to show some value for last 10 seconds, we can do:

        strokeAnimation.keyTimes = [0, 0.25, 0.50, 0.60]
        strokeAnimation.values = [timerFillColor.cgColor,timerFillColor.cgColor, timerFillColor.cgColor, timerFillColor.cgColor, UIColor.red.withAlphaComponent(0.7).cgColor]
strokeAnimation.duration = 25.0 // Let's assume duration is 25.0
  • Related