iosopengl-es-2.0uipinchgesturerecognizerglkview

GLKView zooming over Navigation Bar


I have a GLKView (OpenGL ES2.0) between a navigation bar on the top and a tool bar at the bottom of my iOS app window. I have implemented pinch zoom using UIPinchGestureRecognizer but when I zoom out a good extent, my view runs over the top navigation bar. Surprisingly the view does not go over the tool bar at the bottom. Wonder what I'm doing wrong.

Here's the viewport settings I'm using:

 glViewport(0, 0, self.frame.size.width, self.frame.size.height);

and here's the update and the pinch handler:

-(void) update {    
       float aspect = fabsf(self.bounds.size.width / self.bounds.size.height);

    GLKMatrix4 projectionMatrix = GLKMatrix4MakePerspective(GLKMathDegreesToRadians(65.0f),aspect, 0.01f, 10.0f);
self.effect.transform.projectionMatrix = projectionMatrix;

    GLKMatrix4 modelViewMatrix = GLKMatrix4MakeTranslation(0.0f, 0.0f, -6.0f);
    modelViewMatrix = GLKMatrix4Multiply(modelViewMatrix, _rotMatrix);
    self.effect.transform.modelviewMatrix = modelViewMatrix;
}

-(IBAction) handlePinch: (UIPinchGestureRecognizer *)recognizer {
    recognizer.view.transform = CGAffineTransformScale(recognizer.view.transform, recognizer.scale, recognizer.scale);
    recognizer.scale = 1.0;
}

Solution

  • First, you don't need to call glViewport when drawing with GLKView to its builtin framebuffer -- it does that for you automatically before calling your drawing method (drawRect: in a GLKView subclass, or glkView:drawInRect: if you're doing your drawing from the view's delegate). That's not your problem, though -- it's just redundant state setting (which Instruments or the Xcode Frame Debugger will probably tell you about when you use them).

    If you want to zoom in on the contents of the view rather than resizing the view, you'll need to change how you're drawing those contents. Luckily, you're already set up well for doing that because you're already adjusting the ModelView and Projection matrices in your update method. Those control how vertices are transformed from model to screen space -- and part of that transformation includes a notion of a "camera" you can adjust to affect how near/far the objects in your scene appear.

    In 3D rendering (as in real life), there are two ways to "zoom":