Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 794
Burf2000 Avatar asked Jun 13 '12 14:06

Burf2000


2 Answers

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;
}
like image 59
Burf2000 Avatar answered Oct 10 '22 17:10

Burf2000


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.

like image 32
Lukasz Czerwinski Avatar answered Oct 10 '22 17:10

Lukasz Czerwinski