7 Replies
      Latest reply on Mar 1, 2018 4:32 PM by bram
      bram Level 1 Level 1 (10 points)

        What could be the reason that my GLKViewController's willPause: method is never called?

         

        -(void)glkViewController:(GLKViewController *)controller willPause:(BOOL)pause

         

        I implemented this for my view controller, which derives from GLKViewController.

        But a breakpoint in it, is never hit.

         

        Targetting latest iOS SDK, using Xcode 9, and Objective C.

        • Re: willPause: never called.
          KMT Level 9 Level 9 (14,575 points)

          Using the <> code brackets in the editor, show your rendering loop code relative to calling willPause, thanks.

            • Re: willPause: never called.
              bram Level 1 Level 1 (10 points)

              Thank you, but not quite following you.

               

              The rendering loop is inside GLKViewController class, so not implemented by me.

              The GLKViewController periodically calls update() where I step my simulation, and drawInRect() for the GLKView where I do my rendering.

               

              
              -(void)update
              {
                if ( self.paused )
                return;
                float dt = self.timeSinceLastUpdate;
                dt = ( dt < FLT_EPSILON || dt > 1.0f ) ? 1/60.0f : dt;
                const int numsteps = num_120Hz_steps( dt );
              
                for ( int i=0; i<numsteps; ++i )
                     ctrl_update( 1/120.0f );
              }
              
              -(void)glkView:(GLKView *)view drawInRect:(CGRect)rect
              {
              // My rendering code goes here.
              }
              
              

               

              Subsequently, I added a willPause: method, but unlike the drawInRect() and update(), that one never gets called.

               

              
              -(void)glkViewController:(GLKViewController *)controller willPause:(BOOL)pause
              {
                if ( pause ) return;
                NSUserDefaults* defaults = [ NSUserDefaults standardUserDefaults ];
                NSNumber* n0 = [ defaults objectForKey:@"preference_showfps" ];
                NSNumber* n1 = [ defaults objectForKey:@"preference_sensitivity" ];
                const bool  showfps = n0 ? [ n0 boolValue ] : true;
                const float sensitivity = n1 ? [ n1 floatValue ] : 1.0f;
              
                view_sensitivity = sensitivity;
                ctrl_showfps = showfps;
              }
              
              

               

              I expected willPause: to be called every time the GLKViewController is either shown or hidden.

              But it is not.

               

              I am following Apple's "Drawing with OpenGL ES and GLKit" document which says to use display() and drawInRect().

              [not linking URL here, to avoid getting stuck in moderator-queue forever.]

               

              Thanks again,

               

                Bram

                • Re: willPause: never called.
                  KMT Level 9 Level 9 (14,575 points)

                  Meant 'the' loop used by you, not 'your' loop, sorry.

                   

                  Making the call in viewDidLoad or viewWillAppear? Should be didload, I think.

                   

                  You can break a url to avoid moderation - like this:

                   

                  h ttp://www.iosappreviewpredictiveanalytics.me

                    • Re: willPause: never called.
                      bram Level 1 Level 1 (10 points)

                      The GLKView is created by UIKit, via storyboard at launch.

                      I retrieve it and set some settings on it, in viewDidLoad:

                       

                      
                      -(void)viewDidLoad
                      {
                        [ super viewDidLoad ];
                        self.context = [ [ EAGLContext alloc ] initWithAPI:kEAGLRenderingAPIOpenGLES3 ];
                      
                        GLKView *view = (GLKView *)self.view;
                        view.context = self.context;
                        view.drawableDepthFormat = GLKViewDrawableDepthFormat24;
                      #if TARGET_IPHONE_SIMULATOR
                        view.drawableMultisample = GLKViewDrawableMultisampleNone;
                      #else
                        view.drawableMultisample = GLKViewDrawableMultisample4X;
                      #endif
                        view.multipleTouchEnabled = YES;
                      
                        fbw = self.view.bounds.size.width;
                        fbh = self.view.bounds.size.height;
                      
                        /
                        UIScreen* scn = [ UIScreen mainScreen ];
                        self.preferredFramesPerSecond = [ scn maximumFramesPerSecond ];
                      
                       ...
                      }
                      
                      
                • Re: willPause: never called.
                  bram Level 1 Level 1 (10 points)

                  Ok, I figured out why it was not called.

                   

                  GLKViewController can work in two ways:

                   

                  1. By subclassing GLKViewController and implementing methods for update: and drawInRect:
                  2. By setting a GLKViewControllerDelegate.

                   

                  The two are mutually exclusive.

                   

                  willPause: is a method of the delegate, and not a method of GLKViewController.

                   

                  Because I used subclassing, it will not be called.


                  It looks like I will have to catch the foregrounding/backgrounding in the AppDelegate instead, and find a way to my viewcontroller and have it handle it.