-1

I am new to swift animations and would need some help in understanding this.

So say i have the following code which creates a circle based on the input provided.

    func drawCircle(percentage:CGFloat){
//Some circle drawing code and the circle is drawn using the percentage % provided.

}

i call this function with

drawCircle(0.5)

i will get this. enter image description here i call using

drawCircle(0.75)

i will get this. enter image description here So based on the percentage passed i create the circle/semi-circle/ring. Hope i am making some sense.

Now only part i want to animate is the circle formation. So say when i call drawCircle(0.5) it should animate smoothly from 0.0% to 0.5% Currently if i call my method with a timer and pass the parameters sequentially (0.00, 0.1, 0.2 etc to 0.5) it can work. An example: enter image description here

But I don't think it's the right way to do. So what am looking for is if there is a better way to do this using animations in swift where the transition from 0.0 to 0.5 is smooth.

[I don't want to change my existing codes for the circle creation. Its already an existing code base made by someone i am working and it has some time factor and risk to change anything right now on the existing code. Looking for something where the function "drawCircle(%)" can be used to achieve the desired output. Suggestions are always welcome.]

TIA>

KD.
  • 2,015
  • 3
  • 28
  • 59

1 Answers1

2

Look up how to do animations with UIBezierPath and CAShapeLayer. Basically, you need to create a UIBezierPath with the equation for a circle. Then use the strokeEnd property to define how much of the circle should be animated. Here is an answer for reference, albeit in objective-c

Draw circle with UIBezierPath

Community
  • 1
  • 1
Darvish Kamalia
  • 817
  • 1
  • 7
  • 19
  • Thanks. Ya thats the only way. Have changed my base code and working great. Thanks for your help. – KD. Jul 15 '15 at 08:12