我们以前曾实施攻击Zoom,现在我们决定使用图标,以放大当前显示的内容的中心,我们想重新使用“我们要缩放的代码”,因为我们想要同样的效果,但是现在我们不知道该如何通过中心点。

我们正在使用

(cgrect)Zoomrectforscale :(浮动)比例withCenter :( cgpoint)中心

用于接受我们用于点击缩放的手势识别器中的中心cgpoint的方法;但是,由于我们不再使用手势识别器,因此我们将不得不弄清楚要通过它的cgpoint。另外,此方法适用于Tap Zoom,因此我认为这不是我们遇到问题的地方。

我们尝试这样做

    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);
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top