views:

465

answers:

3

Hi all, I have my app on iPhone with a UIViewController with some stuff inside.. image, textbox, etc... is there a way to draw a line or something like this using opengl directly inside the UIViewController?

thanks in advance

+1  A: 

Are you sure about the OpenGL?

I believe it will be impossible to use OpenGL above regular views.

You might add a custom view (inherit from UIView) above all the other views and draw anything you want on that view.
This view should have a transparent background color.
In addition, if you want to interact with the other views (tap buttons, edit text views, scroll etc.) then you will have to implement the hitTest method in your custom view in order to pass the touch event to the views that are located under this one...

EDIT: Don't mess with the hitTest. Just uncheck the User Interaction Enabled in the XIB...

EDIT: Code sample:

@interface TransparentDrawingView : UIView {
    CGPoint fromPoint;
    CGPoint toPoint;
}
- (void)drawLineFrom:(CGPoint)from to:(CGPoint)to;
@end

@implementation TransparentDrawingView

- (void)initObject {
    // Initialization code
    [super setBackgroundColor:[UIColor clearColor]];
}
- (id)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        // Initialization code
        [self initObject];
    }
    return self;
}
- (id)initWithCoder:(NSCoder *)aCoder {
    if (self = [super initWithCoder:aCoder]) {
        // Initialization code
        [self initObject];
    }
    return self;
}

- (void)drawRect:(CGRect)rect {
    // Drawing code

    // Draw a line from 'fromPoint' to 'toPoint'
}
- (void)drawLineFrom:(CGPoint)from to:(CGPoint)to {
    fromPoint = from;
    toPoint = to;

    // Refresh
    [self setNeedsDisplay];
}

@end
Michael Kessler
Thanks Michael...My goal is to draw a line over an image for example..thanks
ghiboz
See the code sample that I've added to my answer. Use this view as your custom view that will be spread to the entire window above the rest of the views. Fill the drawing code in the `drawRect:` method...
Michael Kessler
+1  A: 

Hi, First I think you should know responsibilities of UIView and UIViewController. UIView is mainly responsible for drawing (override touchBegin, ToucheMove, etc.), animating, manage subviews, and handle event; UIViewController is mainly responsible for loading, unloading views etc.

So, you should 'draw' this line at your customized view (UIView), not view controller.

Second: If you only need to display some simple shapes or lines. I suggest you use UI controls and images. Even 'drawRect' is not recommended since it will cause more resources used. Surely OpenGL need much resources.

Elliot Chen
thanks elliot, and how can I display lines using UI controls???
ghiboz
You can drag a view to be any shape, such as a 100 pix width, 1 pix height rectangle, set its background color as black. it should look like a line. :) And then you can put this 'line' as a subview of your imageview.
Elliot Chen
ok, but if the line needs to be curved??
ghiboz
ghiboz, under that situation i think you shall use a PNG instead. Principle I suggested is: make your solution as simple as you can. We should avoid to use complex method to solve easy problem. :)I did not mean OpenGL should not be used. On the other hand, it's a very powerful tool, but not that easy to handle and will occupy much system resource.
Elliot Chen
A: 

The answer is quite simple, you have to create a new class extending UIView, and override the drawRect function in order to draw (with Quartz...) your line. Use this method if you want gradient background as well.

I am also developping with Adobe Flex 4, and I notice that iPhone SDK has no Skin and Layout "patterns" like Flex 4 does (for instance, UIView could have a Skin (in a separate file..) and a Layout (horizontal, Vertical, Tile....)), for me that is a terrible lack!

Its something that the open source library Three20 tries to bring to the iPhone SDK.

eBuildy