iOS Developer Library

Developer

GLKit Framework Reference GLKView Class Reference

Options
Deployment Target:

On This Page
Language:

GLKView

The GLKView class simplifies the effort required to create an OpenGL ES application by providing a default implementation of an OpenGL ES-aware view. A GLKView directly manages a framebuffer object on your application’s behalf; your application simply needs to draw into the framebuffer when the contents need to be updated.

To use a GLKView in your application, allocate and initialize a new GLKView object and provide it an OpenGL ES context. Then, modify the view’s drawableColorFormat, drawableDepthFormat, drawableStencilFormat, and drawableMultisample properties to configure the format of the drawable’s framebuffer object. After this, the view automatically creates or updates the framebuffer object whenever the view must be redrawn. A GLKView object uses the regular view drawing cycle for a UIView object, calling its drawRect: method whenever the contents of the view need to be updated. Before calling its drawRect: method, the view makes its EAGLContext object the current OpenGL ES context and binds its framebuffer object to the OpenGL ES context as the target for rendering commands. Your application’s implementation of the drawRect: method should call one or more OpenGL ES functions to render an image into the framebuffer object. Then, the view resolves any multisampling that you may have enabled and delivers the finished results.

The GLKView class can be used in conjunction with a GLKViewController object to create an animation rendering loop that redraws the contents of the view at a specified frame rate.

Subclassing Notes

Typically, there is no need to subclass the GLKView class. Instead, provide a delegate object to draw the view’s contents. See GLKViewDelegate Protocol Reference.

Inheritance


Import Statement


Swift

import GLKit

Objective-C

@import GLKit;

Availability


Available in iOS 5.0 and later.
  • Initializes a new view.

    Declaration

    Swift

    init!(frame frame: CGRect, context context: EAGLContext!)

    Objective-C

    - (instancetype)initWithFrame:(CGRect)frame context:(EAGLContext *)context

    Parameters

    frame

    The frame rectangle for the view, measured in points. The origin of the frame is relative to the superview in which you plan to add it. This method uses the frame rectangle to set the center and bounds properties accordingly.

    context

    An OpenGL ES context used to store the framebuffer object.

    Return Value

    An initialized view object or nil if the object couldn't be created.

    Import Statement

    Objective-C

    @import GLKit;

    Swift

    import GLKit

    Availability

    Available in iOS 5.0 and later.

  • delegate delegate Property

    The view’s delegate.

    Declaration

    Swift

    @IBOutlet unowned(unsafe) var delegate: GLKViewDelegate!

    Objective-C

    @property(nonatomic, assign) IBOutlet id<GLKViewDelegate> delegate

    Discussion

    A delegate is optional. If a delegate is provided, it is called instead of calling a drawRect: method whenever the view’s contents need to be drawn. You should either subclass the view to override the drawRect: method, or provide a delegate, but not both.

    Import Statement

    Objective-C

    @import GLKit;

    Swift

    import GLKit

    Availability

    Available in iOS 5.0 and later.

  • The height, in pixels, of the underlying framebuffer object. (read-only)

    Declaration

    Swift

    var drawableHeight: Int { get }

    Objective-C

    @property(nonatomic, readonly) NSInteger drawableHeight

    Discussion

    The height and width of the underlying framebuffer object is calculated automatically by the view object based on its bounds and contentScaleFactor properties and change whenever either of those properties change. Your application never directly adjusts the size of the framebuffer object. Instead, your application should read the drawableHeight and drawableWidth properties and use those to configure its OpenGL ES rendering code. For example, you might use the drawableHeight and drawableWidth properties to set the OpenGL ES viewport, determining the size and complexity of the art assets to load, and so on.

    Import Statement

    Objective-C

    @import GLKit;

    Swift

    import GLKit

    Availability

    Available in iOS 5.0 and later.

    See Also

    drawableWidth

  • The width, in pixels, of the underlying framebuffer object. (read-only)

    Declaration

    Swift

    var drawableWidth: Int { get }

    Objective-C

    @property(nonatomic, readonly) NSInteger drawableWidth

    Discussion

    The height and width of the underlying framebuffer object is calculated automatically by the view object based on its bounds and contentScaleFactor properties and change whenever either of those properties change. Your application never directly adjusts the size of the framebuffer object. Instead, your application should read the drawableHeight and drawableWidth properties and use those to configure its OpenGL ES rendering code. For example, you might use the drawableHeight and drawableWidth properties to set the OpenGL ES viewport, determining the size and complexity of the art assets to load, and so on.

    Import Statement

    Objective-C

    @import GLKit;

    Swift

    import GLKit

    Availability

    Available in iOS 5.0 and later.

    See Also

    drawableHeight

  • context context Property

    The OpenGL ES context used when drawing the view’s contents.

    Declaration

    Swift

    var context: EAGLContext!

    Objective-C

    @property(nonatomic, retain) EAGLContext *context

    Discussion

    The view uses this context as the place to create its underlying framebuffer object and it also sets the context before calling your drawing method. Never change the context from inside your drawing method.

    Import Statement

    Objective-C

    @import GLKit;

    Swift

    import GLKit

    Availability

    Available in iOS 5.0 and later.

  • Binds the underlying framebuffer object to OpenGL ES.

    Declaration

    Swift

    func bindDrawable()

    Objective-C

    - (void)bindDrawable

    Discussion

    Before calling your drawing method, the view binds the underlying framebuffer object to the context so that rendering commands are automatically drawn into it. However, some rendering strategies require you to change the target of your rendering commands to another framebuffer object, such as when you need to render to a texture first. If your application changed the framebuffer object bound to OpenGL ES, it calls this method to rebind the view’s framebuffer object to OpenGL ES.

    Import Statement

    Objective-C

    @import GLKit;

    Swift

    import GLKit

    Availability

    Available in iOS 5.0 and later.

  • A Boolean value that indicates whether the view responds to messages that invalidate the view’s contents.

    Declaration

    Swift

    var enableSetNeedsDisplay: Bool

    Objective-C

    @property(nonatomic) BOOL enableSetNeedsDisplay

    Discussion

    By default, a GLKView object respects the standard view drawing cycle for a UIView object, as described in UIView Class Reference. However, many OpenGL ES applications need to update their contents explicitly in an animation rendering loop. When updating your contents in a rendering loop, the normal on-demand mechanism for view updates can be disabled by setting the value of this property to NOfalse. If your application uses a GLKViewController object to drive the rendering loop, the view controller automatically sets this property to NOfalse.

    Import Statement

    Objective-C

    @import GLKit;

    Swift

    import GLKit

    Availability

    Available in iOS 5.0 and later.

    See Also

    – display

  • Redraws the view’s contents immediately.

    Declaration

    Swift

    func display()

    Objective-C

    - (void)display

    Discussion

    This method causes your drawing method to be called immediately and then presents the rendered image to the screen. Your application typically calls this method inside of a rendering loop, such as the one provided by the GLKViewController class, in order to provide a continuous smooth animation.

    Never call this method inside your drawing function.

    Import Statement

    Objective-C

    @import GLKit;

    Swift

    import GLKit

    Availability

    Available in iOS 5.0 and later.

  • Draws the contents of the view and returns them as a new image object.

    Declaration

    Swift

    var snapshot: UIImage! { get }

    Objective-C

    @property(readonly, strong) UIImage *snapshot

    Return Value

    An image object.

    Discussion

    When this method is called, the view sets up a drawing environment and calls your drawing method. However, instead of presenting the view’s contents on screen, they are returned to your application as an image instead. This method should be called whenever your application explicitly needs the contents of the view; never attempt to directly read the contents of the underlying framebuffer using OpenGL ES functions.

    Never call this method inside your drawing function.

    Import Statement

    Objective-C

    @import GLKit;

    Swift

    import GLKit

    Availability

    Available in iOS 5.0 and later.

  • Deletes the drawable object associated with the view.

    Declaration

    Swift

    func deleteDrawable()

    Objective-C

    - (void)deleteDrawable

    Discussion

    The drawable object consumes a significant amount of memory, so whenever the view is not going to be used to display content for a significant period of time, your application should call this method to dispose of the underlying memory. For example, your application might call this method after hiding a view or before transitioning to another screen of content.

    If you use a GLKViewController object to manage the view, the view controller automatically calls this method whenever your application moves into the background.

    After calling this method, do not invalidate the view’s contents or call the view’s display method until you are ready to display the content again to the user; drawing the view’s contents automatically reallocates the drawable object.

    Import Statement

    Objective-C

    @import GLKit;

    Swift

    import GLKit

    Availability

    Available in iOS 5.0 and later.

  • The format of the color renderbuffer.

    Declaration

    Swift

    enum GLKViewDrawableColorFormat : GLint { case RGBA8888 case RGB565 case SRGBA8888 }

    Objective-C

    typedef enum { GLKViewDrawableColorFormatRGBA8888 = 0, GLKViewDrawableColorFormatRGB565, GLKViewDrawableColorFormatSRGBA8888 } GLKViewDrawableColorFormat;

    Constants

    • RGBA8888

      GLKViewDrawableColorFormatRGBA8888

      An RGBA8888 format.

      Available in iOS 5.0 and later.

    • RGB565

      GLKViewDrawableColorFormatRGB565

      An RGB565 format.

      Available in iOS 5.0 and later.

    • SRGBA8888

      GLKViewDrawableColorFormatSRGBA8888

      An sRGBA8888 format.

      Available in iOS 5.0 and later.

    Import Statement

    Objective-C

    @import GLKit;

    Swift

    import GLKit

    Availability

    Available in iOS 5.0 and later.

  • The format of the depth renderbuffer.

    Declaration

    Swift

    enum GLKViewDrawableDepthFormat : GLint { case FormatNone case Format16 case Format24 }

    Objective-C

    typedef enum { GLKViewDrawableDepthFormatNone = 0, GLKViewDrawableDepthFormat16, GLKViewDrawableDepthFormat24, } GLKViewDrawableDepthFormat;

    Constants

    • FormatNone

      GLKViewDrawableDepthFormatNone

      The underlying framebuffer object has no depth buffer.

      Available in iOS 5.0 and later.

    • Format16

      GLKViewDrawableDepthFormat16

      A 16-bit depth entry for each pixel.

      Available in iOS 5.0 and later.

    • Format24

      GLKViewDrawableDepthFormat24

      A 24-bit depth entry for each pixel.

      Available in iOS 5.0 and later.

    Import Statement

    Objective-C

    @import GLKit;

    Swift

    import GLKit

    Availability

    Available in iOS 5.0 and later.

  • The format of the stencil renderbuffer.

    Declaration

    Swift

    enum GLKViewDrawableStencilFormat : GLint { case FormatNone case Format8 }

    Objective-C

    typedef enum { GLKViewDrawableStencilFormatNone = 0, GLKViewDrawableStencilFormat8, } GLKViewDrawableStencilFormat;

    Constants

    • FormatNone

      GLKViewDrawableStencilFormatNone

      The underlying framebuffer object has no stencil buffer.

      Available in iOS 5.0 and later.

    • Format8

      GLKViewDrawableStencilFormat8

      An 8-bit stencil entry for each pixel.

      Available in iOS 5.0 and later.

    Import Statement

    Objective-C

    @import GLKit;

    Swift

    import GLKit

    Availability

    Available in iOS 5.0 and later.

  • The format of the multisampling buffer.

    Declaration

    Swift

    enum GLKViewDrawableMultisample : GLint { case MultisampleNone case Multisample4X }

    Objective-C

    typedef enum { GLKViewDrawableMultisampleNone = 0, GLKViewDrawableMultisample4X, } GLKViewDrawableMultisample;

    Constants

    • MultisampleNone

      GLKViewDrawableMultisampleNone

      Multisampling is not enabled.

      Available in iOS 5.0 and later.

    • Multisample4X

      GLKViewDrawableMultisample4X

      Multisampling is enabled.

      Available in iOS 5.0 and later.

    Discussion

    Multisampling improves the quality of the output image, but may require more memory and image processing to do so. As such, you should profile your application with and without multisampling enabled, and choose a setting that provides both the image quality and performance you require.

    Import Statement

    Objective-C

    @import GLKit;

    Swift

    import GLKit

    Availability

    Available in iOS 5.0 and later.