简体   繁体   中英

UIScrollView Snapping with OpenGL GLKit

This has been a bit of a interesting one.

So I have a OpenGl screen with a scrollview on top. I Use a CADisplay link to update the render when I scroll (code below) which works really well.

However, I can't get it to snap to a point (animated). I believe the releasing of the CADisplay link stops the animation of the scrollview (see snapToItem).

I tried firing off the release of the CADisplay 2 seconds after but that causes other issues.

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    moveFactor = 0 - (((self.scrollView.contentSize.height - self.scrollView.frame.size.height) - self.scrollView.contentOffset.y) / itemScrollViewMoveFactor);
    [self updateLabelPositionScale];
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    [self startDisplayLinkIfNeeded];
}

- (void)snapToItem
{
    NSLog(@"%d", self.selectItem);
    [self.scrollView setContentOffset:CGPointMake(0, (self.scrollView.contentSize.height - self.scrollView.frame.size.height) - (itemScrollViewHeight * self.selectItem)) animated:YES];
    //[NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(stopDisplayLink)  userInfo:nil repeats:NO];
    [self stopDisplayLink];
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    if (!decelerate) 
    {
        [self snapToItem];
    }
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    [self snapToItem];
}

#pragma mark Display Link

- (void)startDisplayLinkIfNeeded
{
    if (!self.displayLink) 
    {
        self.displayLink = [CADisplayLink displayLinkWithTarget:self.view selector:@selector(display)];
        [self.displayLink addToRunLoop:[NSRunLoop mainRunLoop] forMode:UITrackingRunLoopMode];
    }
}

- (void)stopDisplayLink
{
    if (self.displayLink)
    {
        [self.displayLink invalidate];
        self.displayLink = nil;
    }
}

您是否尝试将显示链接添加到NSRunLoopCommonModes?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM