2

i am using UICircularProgressView and have changed it to my liking. the initialization is clear but i'll post it anyways:

UICircularProgressView *wheelProgress = [[UICircularProgressView alloc]initWithFrame:CGRectMake(someFrame)];

then i am setting the right progress CGFloat between 0-1

CGFloat progress = .3;   // example
[self.wheelProgress setProgress:progress];

This works just fine, and looks good, but i would really like to somehow animate it. I havent worked with animations so far, so my first approach was something like

for(tempProgress =! progress){
  incrementing tempProgress
  setting tempProgres to progressView
}

this of course is massively ugly and blocks everything else going on.

what i would like to achieve is a linear animation of the progress from 0 to its final value.

i have looked at: Animation tutorial

and came up with something like this:

CGContextRef context = UIGraphicsGetCurrentContext();
[UIView beginAnimations:nil context:context];
[UIView setAnimationCurve:UIViewAnimationCurveLinear];
[UIView setAnimationDuration: 1];
wheelProgress.progress = (progress) ? progress : 0.0;
[UIView commitAnimations];

but somehow this does not work..

any ideas what i need to fix here?

thanks in advance sebastian

Sebastian Flückiger
  • 5,525
  • 8
  • 33
  • 69
  • just use your increment value be very small in order of 0.01f or less so you get what you want – The iOSDev Apr 24 '12 at 09:06
  • if i do that the whole application gets blocked for the duration of the animation because i'd have to do it in the main thread that blocks the interface when busy.. im looking for a more elegant solution.. – Sebastian Flückiger Apr 24 '12 at 09:30

1 Answers1

1

The progress view you are using Is not set up to have the progress as a animatable property. To do so is quite complex and I'm not sure it is what you are looking for.

If it is, I think you would need to do the following (I haven't done this myself, this is just based on documentation and books that I have read):

  1. Rewrite the view to use CALayers and drawInContext instead of drawRect
  2. Make progress a property on the layer (use @dynamic instead of @synthesize
  3. Override actionForKey: on the layer subclass and return a CABasicAnimation for the key @"progress"

Examples of this can be found in a few SO question / answers: here and here should get you started or at least used to the search terms that can be used. This looks like a good tutorial for drawing animated slices in a pie chart using layers, which may be closer to what you want than starting with the UICircularProgressView.

Alternatively, you could implement this instead using an NSTimer which increases the progress value every 1/60th of a second, until it hits your desired value.

Community
  • 1
  • 1
jrturton
  • 118,105
  • 32
  • 252
  • 268
  • havent thought of that - i'll give it a try and post my findings. in what way would it be complex? im actually not afraid to try out something complex if its the best solution im very eager to learn ;-) could you maybe point me in the right direction on how to make a property `animatable` ? – Sebastian Flückiger Apr 24 '12 at 09:32
  • I've added a few details and links, the tutorial looks like it covers almost everything you need. Good luck! – jrturton Apr 24 '12 at 09:52