5

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

Community
  • 1
  • 1
Burf2000
  • 5,001
  • 14
  • 58
  • 117
  • No idea but I'm very interested in the answer as I'm cooking something similar. I wonder if kicking the UIScollViews drawRect (indirectly through setNeedsDisplay) out of the main thread might do it. Up till now I've done all UI stuff strictly in the main thread. – Patrick Borkowicz Jun 13 '12 at 16:41

2 Answers2

3

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;
}
Burf2000
  • 5,001
  • 14
  • 58
  • 117
2

Please refer to the session 223 of Apple WWDC 2012. Starting from 21:21 they talk about integrating UIScrollView with OpenGL view. The interesting part starts at 25:53.

The main source code was written by Burf2000, but this video explains WHY it's working, how UIScrollView really works and how it might interact with OpenGL. It also helped me debug my code.

Lukasz Czerwinski
  • 13,499
  • 10
  • 55
  • 65
  • "source code was written by Burf2000 .." The source code was provided by Apple as sample code. Search for OpenGLScroller for full working example. – chunkyguy May 11 '21 at 06:49