简体   繁体   English

使用OpenGL GLKit进行UIScrollView捕捉

[英]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. 因此,我有一个带有顶部滚动视图的OpenGl屏幕。 I Use a CADisplay link to update the render when I scroll (code below) which works really well. 滚动时(下面的代码),我使用CADisplay链接更新渲染,效果很好。

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). 我相信CADisplay链接的释放会停止滚动视图的动画(请参见snapToItem)。

I tried firing off the release of the CADisplay 2 seconds after but that causes other issues. 我尝试在2秒后启动CADisplay的发行版,但这会导致其他问题。

- (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?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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