views:

324

answers:

1

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
I get these errors. http://grab.by/1t5e
Joshua
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
I've put it into the touchesEnd: method of an UIView.
Joshua
-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
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
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
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
And even when I put it into a drawRect: method I get the same error.
Joshua
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
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
I see, How would I layer UIViews then?
Joshua
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