2011-04-27 44 views
2

我們之前實現了點擊縮放功能,現在我們決定使用圖標來放大當前正在顯示的內容的中心位置,並且我們希望重複使用我們的點擊縮放代碼,因爲我們想要達到同樣的效果,但現在我們不知道要通過什麼作爲中心點。如何使用滾動視圖縮放到中心?

我們正在使用的

(的CGRect)zoomRectForScale:(浮)標withCenter:(CGPoint)中心

方法,用來接受來自我們使用的自來水縮放手勢識別中心cgpoint ;然而,由於我們不再使用手勢識別器,我們將不得不找出通過它的cgpoint。此外,這種方法適用於水龍頭縮放,所以我不認爲這是我們遇到問題的地方。

我們試着這樣做

centerPoint = [scrollView contentOffset]; 
    centerPoint.x += [scrollView frame].size.width/2; 
    centerPoint.y += [scrollView frame].size.height/2; 
    CGRect zoomRect = [self zoomRectForScale:newScale withCenter:centerPoint]; 

哪些應該計算出當前的中心,然後把它傳遞給zoomRectForScale,但它不工作(它放大的方式向中心的右側)。

我認爲這個問題可能與我們在應用縮放之前傳遞圖像中心這一事實有關,也許我們應該傳遞縮放的中心。有沒有人有這方面的經驗,或對我們應該如何計算中心有任何想法?

回答

1

我們得到了它的工作,我想我會發布什麼,我們最終做

/** 
Function for the scrollview to be able to zoom out 
**/ 

-(IBAction)zoomOut { 

    float newScale = [scrollView zoomScale]/ZOOM_STEP; 
    [self handleZoomWith:newScale andZoomType: FALSE]; 
} 

/** 
Function for the scrollview to be able to zoom in 
**/ 

-(IBAction)zoomIn { 

    float newScale = [scrollView zoomScale] * ZOOM_STEP; 
    [self handleZoomWith:newScale andZoomType: TRUE]; 
} 

-(void)handleZoomWith: (float) newScale andZoomType:(BOOL) isZoomIn { 

    CGPoint newOrigin = [zoomHandler getNewOriginFromViewLocation: [scrollView contentOffset] 
                 viewSize: scrSize andZoomType: isZoomIn]; 
    CGRect zoomRect = [self zoomRectForScale:newScale withCenter:newOrigin]; 
    [scrollView zoomToRect:zoomRect animated:YES]; 
} 

,然後在zoomHandler類,我們有這個

-(CGPoint) getNewOriginFromViewLocation: (CGPoint) oldOrigin 
           viewSize: (CGPoint) viewSize 
          andZoomType:(BOOL) isZoomIn { 

    /* calculate original center (add the half of the width/height of the screen) */ 
    float oldCenterX = oldOrigin.x + (viewSize.x/2); 
    float oldCenterY = oldOrigin.y + (viewSize.y/2); 

    /* calculate the new center */ 
    CGPoint newCenter; 
    if(isZoomIn) { 
     newCenter = CGPointMake(oldCenterX * zoomLevel, oldCenterY * zoomLevel); 
    } else { 
     newCenter = CGPointMake(oldCenterX/zoomLevel, oldCenterY/zoomLevel); 
    } 

    /* calculate the new origin (deduct the half of the width/height of the screen) */ 
    float newOriginX = newCenter.x - (viewSize.x/2); 
    float newOriginY = newCenter.y - (viewSize.y/2); 

    return CGPointMake(newOriginX, newOriginY); 
}