Search code examples
iphoneiosopengl-esuiscrollviewglkit

IOS : UIScrollView and OpenGL


So I have a OpenGL(glView) view that is rendering a menu which I aim to scroll. I was trying to avoid reinventing the UIScrollView and so I have place a scrollview on top of the glView.

The issue is that scrolling the scrollview pauses the rendering

A similar issue was discussed here Animation in OpenGL ES view freezes when UIScrollView is dragged on iPhone

Problem is I have no idea what [displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes]; refers to

I have made a new CADisplayLink and tried to do the above with no luck

I have tried calling the render method in the scrollViewDidScroll

I have tried calling [self.view setNeedsDisplay];

I also found a referring to calling the timerLoop?

Can anyone help me out please


Solution

  • So, I have found a solution :)

    create a CADisplayLink *_displayLink; property (you need import QuartzCore)

    Have your scrollview on top.

        #pragma mark - scrollView Delegate
    
    - (void)scrollViewDidScroll:(UIScrollView *)scrollView
    {
        //code for moving the object here    
    }
    
    - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
    {
        [self startDisplayLinkIfNeeded];
    }
    
    - (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
    {
        if (!decelerate) {
            [self stopDisplayLink];
        }
    }
    
    - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
    {
        [self stopDisplayLink];
    }
    
    #pragma mark Display Link
    
    - (void)startDisplayLinkIfNeeded
    {
        if (!_displayLink) {
            // do not change the method it calls as its part of the GLKView
            _displayLink = [CADisplayLink displayLinkWithTarget:self.view selector:@selector(display)];
            [_displayLink addToRunLoop:[NSRunLoop mainRunLoop] forMode:UITrackingRunLoopMode];
        }
    }
    
    - (void)stopDisplayLink
    {
        [_displayLink invalidate];
        _displayLink = nil;
    }