2012-02-20 59 views
3

我在iPad上創建一個繪圖應用程序,用戶可以在繪圖中滾動並滾動。 (想象一下畫布寬度爲4000像素,視圖端口寬度爲1024)目前,我使用OpenGL繪製圖形,寬度爲1024,效果很好。當我將UIView的幀大小更改爲4000時,出現「未完成幀緩衝區對象8cd6」。當我將它縮小到2000的寬度時,我最終得到了「古怪」的結果。我知道我可以正確操作框架,因爲框架寬度爲500會產生正確的結果。iOS OpenGL圖層和UIScrollView

我也在考慮留下寬度1024並移動OpenGL層的相機,但是如何使用我設置的UIScrollView?所以我現在不確定該做什麼。有什麼建議?

在此先感謝

P.S.代碼主要基於GLPaint示例Apple提供的here

+0

嗨,我正在研究同樣的問題,但我無法讓我的實施工作。你有沒有設法解決你的問題? – Gabor 2016-05-12 14:53:15

回答

4

我認爲你最好用最後建議的方案 - 將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重繪。

2

我強烈建議您使用OpenGL相機進行縮放,而不是嘗試在UIScrollView中使用它。這將是一個更多的工作來建立,但最終走向恕我直言。