如何使用滚动视图缩放到中心?
问题描述:
我们之前实现了点击缩放功能,现在我们决定使用图标来放大当前正在显示的内容的中心位置,并且我们希望重复使用我们的点击缩放代码,因为我们想要达到同样的效果,但现在我们不知道要通过什么作为中心点。如何使用滚动视图缩放到中心?
我们正在使用的
(的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,但它不工作(它放大的方式向中心的右侧)。
我认为这个问题可能与我们在应用缩放之前传递图像中心这一事实有关,也许我们应该传递缩放的中心。有没有人有这方面的经验,或对我们应该如何计算中心有任何想法?
答
我们得到了它的工作,我想我会发布什么,我们最终做
/**
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);
}