0

I'm trying to draw an animated growing line using Quartz 2d, by adding points to an existing line, gradually over time. I started drawing a new line, In the drawRect method of a UIView, by obtaining the CGContextRef, setting its draw properties, and moving the first point to (0,0).

CGContextRef context= UIGraphicsGetCurrentContext();
CGContextSetStrokeColorWithColor(context,[UIColor blueColor].CGColor);
CGContextSetLineWidth(context, 2);
CGContextMoveToPoint(context,0,0);

later, in my next drawRect call, i tried extending that line, by again, obtaining the CGContextRef, and adding a new point to it.

GContextRef context= UIGraphicsGetCurrentContext();
CGContextAddLineToPoint(context,x,y);

but it seems that the current CGContextRef doesn't have any record of my previous CGContextMoveToPoint command from the last drawRect call, therefore doesn't have any reference that i already started drawing a line.

Am i doing something wrong here? is there a way refering an already drawn line?

Dubon Ya'ar
  • 349
  • 4
  • 13
  • thanks, i'm surprised Apple's documentation doesn't include any reference to that specific behavior, like it's obvious... – Dubon Ya'ar Dec 26 '10 at 11:27

2 Answers2

1

You basically need to treat each call to drawRect as if it was starting from scratch. Even if you are only asked to update a subrect of the view, you should assume that any state associated with the graphics context, such as drawing position and colours, will have been reset. So in your case, you need to keep track of the start position and redraw the whole line each time.

walkytalky
  • 9,453
  • 2
  • 36
  • 44
0

I think the better approach is to animate some thin UIView. Look my answer here.
If you need more than just horizontal line, rotate that view. I think it's better for the performance.

Community
  • 1
  • 1
DanSkeel
  • 3,853
  • 35
  • 54
  • Not only are you are wrong about the best strategy -- abusing the view hierarchy is rarely the right way to draw stuff -- you also seem to be have missed the point of the question... – walkytalky May 16 '12 at 10:22
  • Yes, you're right, I missed the point(. Do you think that your answer is also good for solving this [problem](http://stackoverflow.com/questions/10603391/ios-animating-the-drawing-of-a-line/10603854#10603854)? I thought I shouldn't redraw too frequently? I will delete this answer. – DanSkeel May 16 '12 at 12:15
  • You shouldn't redraw too frequently, but also you can't redraw less frequently than necessary. You may be able to get something else to do the drawing for you -- like a view -- but the drawing still has to take place. The view code may or may not be better optimised than what you write yourself, but that depends on how good the fit is between what it's designed to do and what you're trying to do with it. (contd...) – walkytalky May 17 '12 at 10:08
  • In general you have to make a choice about what to draw for yourself and what to defer to system components like UIView. At one extreme you could explicitly draw your whole UI and do all the event despatch etc; on the other, you could end up painting an image by using a different 1x1 UIView for each coloured pixel. In reality you will usually want to find a happy medium (although it's worth noting that graphical games often tend towards the former approach). Exactly where that is varies with the problem (and also the habits of the coder). (...) – walkytalky May 17 '12 at 10:14
  • In the case of the question pointed to, your UIView approach could make sense, but it depends on context. (Of course, the additional context is that the OP *wants* to do explicit Quartz2D drawing.) If this was happening in a form mostly made of system UI components with no other custom drawing, then letting a view be the line is reasonable. But you shouldn't do it if, for example, you're drawing a a diagram consisting of many lines and shapes to correspond to some data you are creating or maintaining. (...) – walkytalky May 17 '12 at 10:19
  • In that case, you should certainly take responsibility for the drawing yourself. This is the situation my answer is relevant to, but it is not saying *how* to do it, only explaining part of the situation that exists when you do. So it does not provide an answer to that other question, which seems to me more concerned with how to drive an explicitly drawn graphics path from CoreAnimation. (I'll stop now!) – walkytalky May 17 '12 at 10:27
  • Your call. I agree it's all a bit of a sidetrack here, but probably almost no-one will ever read or care about it either way... – walkytalky May 17 '12 at 21:48