4

I have custom drawing code that uses bezier paths , gradients and strokes to perform my drawing. I want to run custom animations by looping drawRect and changing the values of properties on the bezier paths.

I have looked at using CAShapeLayer (UIView animation in drawrect) but that doesn't seem to cut it for me. My drawing code is quite complex, runs into a few hundred lines and all the drawing is done through bezier paths and gradients. Changing the drawings to a CAShapeLayer and then adding colors and gradients to it will be very time consuming!

I know that it is not recommended by Apple to explicitly call drawRect rather to use setNeedsDisplay to call draw rect (How to use DrawRect correctly). But the problem with doing that is i experience slight difference in the animation each time (very minute though). It may have something to do with the fact that setNeedsDisplay schedules drawRect to be called on the run loop but doesn't directly call it by itself.

I want to know what strategies i can use to use to loop drawRect and achieve synchronized perfectly timed animations each time. Is it possible to do this?

Community
  • 1
  • 1
Sagar
  • 555
  • 9
  • 24

1 Answers1

3

Both Animating Pie Slices Using a Custom CALayer and Animating Custom Layer Properties by Rob Napier are two good resources to learn how to make custom animations when you are doing completely custom drawing inside drawInContext:.

If you still feel that setting up an external mechanism to synchronize the drawing then I suggest that you look at CADisplayLink.

David Rönnqvist
  • 56,267
  • 18
  • 167
  • 205
  • So im assuming i can encapsulate the drawing in a CALayer (in drawInContext)and use CABasicAnimations to get the animation done based on what properties in the Layer im changing? And maybe use a timeSinceLastDraw from the displayLink to synchronize the animations? Is this a good way to go about it? – Sagar Oct 17 '13 at 08:34
  • Yes. I'm sure you can adapt the code from these two resources to your custom drawing. Core Animation is the framework you should be using for most advanced animations. – David Rönnqvist Oct 17 '13 at 08:39
  • Thank you! I had seen the pie slices tutorial before but the one by Rob is really helpful to. Will give it a try! – Sagar Oct 17 '13 at 08:41