1

I need to continuously change the color of all the lines I draw in a UIView. And I drew all the lines within the drawrect method to assign colors. Now I need to animation and change the colors as long as the view is shown. Is there a way to do it? Thanks a lot!

trillions
  • 3,669
  • 10
  • 40
  • 59

2 Answers2

3

Look into using one instance of CAShapeLayer for each of the lines, instead of drawing them in drawRect:. You can animate changes to each layer's path and strokeColor properties as necessary.

EDIT

If you're just drawing horizontal lines, you may be better off simply using a UIView for each. Set the backgroundColor instead of implementing drawRect:. You can animate both the frame property and the backgroundColor property using +[UIView animateWithDuration:animations:] (or the more powerful variants of it). This is generally simpler than messing around with Core Animation.

rob mayoff
  • 375,296
  • 67
  • 796
  • 848
  • A `CGPath` can contain multiple disconnected subpaths, but I explicitly suggested using a separate layer for each line. – rob mayoff Mar 12 '13 at 07:47
  • wish we three can have a quick chat..shall i go with CAShapeLayer or UIView? – trillions Mar 12 '13 at 07:47
  • If the OP doesn't need anything other than a straight line, then you don't need to use a `CAShapeLayer`, I'm pretty sure. – Jacob Relkin Mar 12 '13 at 07:48
  • yea, since each line will need to change its color, i'd better use different layers... – trillions Mar 12 '13 at 07:48
  • If a line is straight but diagonal, it may be easier to use a `CAShapeLayer` than a transformed `UIView`. – rob mayoff Mar 12 '13 at 07:49
  • @nanshi Check out [this tutorial](http://nachbaur.com/blog/core-animation-part-4). It looks helpful. – rob mayoff Mar 12 '13 at 07:49
  • rob, i m drawing horizontal lines in different colors, then need to using animation to change its color when the view is shown. let me check out the tutorial you pointed me to first. Thanks a lot! :) – trillions Mar 12 '13 at 07:50
  • Thanks a lot! I've read it. How about the performance part? I think i will go with the UIView as both of you suggested. And I will then can change both color and height of the lines :) super cool~ thank you sooo much ^___^ – trillions Mar 12 '13 at 07:56
  • 1
    You should find the performance adequate. If it's not, switching from `UIView` to a layer won't make much difference, because the features of `UIView` that you're using are just a very thin layer on top of the view's underlaying `CALayer`. If you're trying to animate hundreds of lines at once, you may need to use OpenGL ES to get sufficient performance. – rob mayoff Mar 12 '13 at 07:59
  • rob, true that i may have to draw more than 100 lines. Maybe let me go with UIViews first and see how it goes. btw, how hard it is to do it in OpenGL ES? – trillions Mar 12 '13 at 17:10
  • If you have never used OpenGL, you will find it difficult. – rob mayoff Mar 12 '13 at 17:14
2

Live and learn. Animations are better done outside of -drawRect:

Change your "lines" to individual UIView instances added as (preferably opaque) subviews to your view. You'd then set the appropriate backgroundColor on the view.

You can then use a simple UIView animation to animate them.

Jacob Relkin
  • 161,348
  • 33
  • 346
  • 320
  • Thanks for your suggestion! :) Is it better for me to use CALayers or UIViews, in your opinion? Yeah...i realized i can't animate it within drawRect..just dont know what's the proper way to do. Basically on animation, i need to change the color of the lines... – trillions Mar 12 '13 at 07:45