我認爲你最好用最後建議的方案 - 將OpenGL視圖保持爲靜態並在滾動視圖之外,並移動其內容以匹配滾動視圖的移動。
假設你正在使用GLKView
,實現您的glkView:drawInRect:
,使其得到contentOffset
(和可能的bounds
)從您的滾動視圖屬性,並繪製適當。例如。 (假裝你使用GLES 1.0只是因爲矩陣操作方法是那麼有名):
- (void)glkView:(GLKView *)view drawInRect:(CGRect)rect
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// displayArea will be the area the scroll view is
// currently displaying; taking just the bounds would
// likely be fine too
CGRect displayArea;
displayArea.origin = scrollView.contentOffset;
displayArea.size = scrollView.bounds.size;
// assuming (0, 0) in the GL view is in the centre,
// we'll adjust things so that it's in the corner ala
// UIKit
CGPoint centre = CGPointMake(
displayArea.origin.x + displayArea.size.width*0.5f,
displayArea.origin.y + displayArea.size.height*0.5f);
glPushMatrix();
// apply the scroll as per the scroll view
// so that its centre is aligned with our centre
glTranslatef(-centre.x, -centre.y, -1);
/* rest of drawing here */
glPopMatrix();
}
然後自己連作爲代表到滾動視圖,只是執行:
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
[glView setNeedsDisplay];
}
爲了保證當滾動視圖滾動時,GL重繪。
嗨,我正在研究同樣的問題,但我無法讓我的實施工作。你有沒有設法解決你的問題? – Gabor 2016-05-12 14:53:15