PDA

View Full Version : CGContextRef




sujithkrishnan
Dec 1, 2008, 07:57 AM
Hi all,
i have drawn ellipse in my view,when user touches within that ellipse i should go on drawing eclipse with increasing radius.How to achive this.CGContextRef doesn't work when i try to re draw the ellipse in buttonPressed event

We can call setNeedsDisplay, but it will call the drawRect with old parameters (radius1 and radius2), how to pass new radius to drawRect and draw the ellipse?



robbieduncan
Dec 1, 2008, 08:20 AM
1) Whilst this makes no difference I think you mean ellipse, not eclipse

2) Where are you getting this CGContextRef from? If you are trying to draw outside of your drawRect: method, or calling drawRect: directly instead of calling the correct method to ask the view to redraw this is probably the issue.

firewood
Dec 1, 2008, 09:34 AM
Unlike other programming environments with direct graphics calls, in CocoaTouch, you don't draw directly when you want to.

Instead, you tell the OS you need to draw (using the setNeedsDisplay method on the view that needs drawing, then exit your current method back to the run loop). When the OS decides it's time to draw, it will call that view's drawRect method, and then that method can draw in the context it's given.

The basic paradigm for a lot of Cocoa is that your program doesn't call the OS. Instead, you make your program a bunch of modules that the OS calls when it's good and ready.


.

sujithkrishnan
Dec 1, 2008, 11:24 PM
Unlike other programming environments with direct graphics calls, in CocoaTouch, you don't draw directly when you want to.

Instead, you tell the OS you need to draw (using the setNeedsDisplay method on the view that needs drawing, then exit your current method back to the run loop). When the OS decides it's time to draw, it will call that view's drawRect method, and then that method can draw in the context it's given.

The basic paradigm for a lot of Cocoa is that your program doesn't call the OS. Instead, you make your program a bunch of modules that the OS calls when it's good and ready.


.


Prob is that we need a context to push to the stack. In case of drawRect OS will do that, If we want to do it manually, options are there some CGContextPush / CGCOntextPop methods are there.
I just want to know how to make use of that?

keehun
Dec 2, 2008, 12:47 AM
I think a better way would be subclass a UIView and draw directly in that. Then add the subclass as a subview. UIView has very good added bonus, like touch handling, etc.

firewood
Dec 2, 2008, 01:40 AM
If you are trying to get a context to draw inside your buttonPressed handler, you haven't yet learned the CocoaTouch paradigm. It is not guaranteed that the graphics chip is accessible at all by the CPU inside CocoaTouch UI event handlers.

Decide what you need to draw, update your parameters, exit your button handler, and then draw later when drawRect gets called.

.