Search code examples
iosopengl-esuiviewcore-animationdrawrect

how to draw opengl es in a UIView


I want to draw my opengl es animation in a customed UIView, how can I do it. I find that I need to override the drawRect method. Which is in the below.

#import "myView.h"

@implementation myView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
    // Initialization code
    }
    return self;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

@end

I have draw a View using interface builder, how can I link to that view and draw(call) my class of opengl animation. Can anyone please write a little bit specific. I'm a beginner. Thanks.


Solution

  • As the other poster said, you don't use drawRect to implement OpenGL in a view. You attach an OpenGL layer to the view.

    EAGLView is one way. If you're willing to limit your app to iOS 5 or later, you might want to look at GLKit. That does a lot of the setup for you.

    Beware, though. OpenGL is in no way, shape, or form a beginner's API. It's big, complex, and tricky to use. There is a HUGE learning curve. I've been programming for about 30 years, and it just about made my head explode when I first started studying it.