I have 2 CGPoints and a would like to draw straight line between them, how would I do this?
+5
A:
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextMoveToPoint(ctx, point1.x, point1.y);
CGContextAddLineToPoint(ctx, point2.x, point2.y);
CGContextStrokePath(ctx);
dancavallaro
2009-12-31 06:55:58
I get these errors. http://grab.by/1t5e
Joshua
2009-12-31 07:22:02
Joshua: Well, read what they say. The most likely reason why the context is invalid is that you haven't pasted this code into the correct place. Try putting the code into a method where there would be a valid current drawing context.
Peter Hosey
2009-12-31 11:46:44
I've put it into the touchesEnd: method of an UIView.
Joshua
2009-12-31 11:50:29
-touchesEnded: is not a location where you have a valid current drawing context. What Peter's referring to is somewhere like -drawRect: of a custom UIView subclass. You then would trigger the redraw of a view using -setNeedsDisplay. The UIView class reference has more: http://developer.apple.com/iPhone/library/documentation/UIKit/Reference/UIView_Class/UIView/UIView.html#//apple_ref/occ/instm/UIView/drawRect:
Brad Larson
2009-12-31 13:41:55
I've put it into a new method, following your suggestion, as you can see here http://grab.by/1teD, but it still doesn't work.
Joshua
2009-12-31 13:54:20
I don't think you understood my comment. Quartz drawing for display to the screen needs to be done in a specific place within a UIView subclass (within -drawRect:), and nowhere else. Read the following section of the iPhone Application Programming Guide for an example of this: http://developer.apple.com/iPhone/library/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/WindowsandViews/WindowsandViews.html#//apple_ref/doc/uid/TP40007072-CH8-SW15
Brad Larson
2009-12-31 18:07:25
Oh. But what about if you want to trigger more than one thing which draws, for example my touchesMoved: method draws a line but I also need to use drawRect: to draw a gradient background. How do I separate them?
Joshua
2009-12-31 18:17:19
And even when I put it into a drawRect: method I get the same error.
Joshua
2009-12-31 18:25:25
There are a couple of ways of approaching drawing multiple things: different UIViews for different drawing elements that are layered on top of one another (my recommendation, because you don't need to redraw all items any time a single one changes) or having the drawing code for all items within a single -drawRect: method. The Quartz drawing calls are executed in order, with later items overlaying earlier ones.
Brad Larson
2009-12-31 20:19:29
In regards to the drawing errors, be sure that you are not calling drawRect: directly. If you need to refresh the UIView, call -setNeedsDisplay on the view instead.
Brad Larson
2009-12-31 21:27:47
I see, How would I layer UIViews then?
Joshua
2010-01-01 07:53:52
Place one UIView on top of another, either by making the overlay view a subview of the underlying view or by putting both at the same location in a shared superview, with one ordered before the other. I suggest reading the following section of the iPhone Application Programming Guide: http://developer.apple.com/iphone/library/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/WindowsandViews/WindowsandViews.html#//apple_ref/doc/uid/TP40007072-CH8-SW47
Brad Larson
2010-01-01 19:34:35