如何用滚动视图缩放到中心?

发布于 2024-11-03 19:35:11 字数 723 浏览 0 评论 0原文

我们之前已经实现了点击缩放,现在我们决定使用图标来放大当前显示内容的中心,并且我们希望重用点击缩放的代码,因为我们想要相同的效果,但现在我们不知道要传递什么作为中心点。

我们使用

(CGRect)zoomRectForScale:(float)scale withCenter:(CGPoint)center

方法,该方法用于接受来自手势识别器的中心 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,但是它不起作用(它缩放到中心的右侧)。

我认为这个问题可能与我们在应用缩放之前传递图像的中心这一事实有关,也许我们应该传递一个缩放的中心。有谁有这方面的经验,或者对我们应该如何计算中心有任何想法?

We had previously implemented tapping to zoom and now we've decided to use icons instead that will zoom in on the center of whats currently being displayed, and we'd like to reuse the code we had for our tap to zoom since we want the same effect, but now we don't know what to pass as the centerpoint.

We're using the

(CGRect)zoomRectForScale:(float)scale withCenter:(CGPoint)center

method which used to accept a center cgpoint from the gesture recognizer that we used for tap zooming; however, since we're no longer using the gesture recognizer we're going to have to figure out what cgpoint to pass it. Also, this method worked for the tap zoom so I don't think this is where we're having the problem.

We tried doing this

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

Which should calculate the current center and then pass it to zoomRectForScale, however it doesn't work (it zooms way to the right of the center).

I think that the problem probably has something to do with the fact that we're passing the center of the image before zoom is applied, and perhaps we're supposed to be passing a scaled center. Does anyone have any experience with this, or have any ideas on how we should be calculating the center?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

猫九 2024-11-10 19:35:11

我们让它工作了,我想我应该发布我们最终做了什么

 /**
 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);
}

We got it working and I thought I'd post what we ended up doing

 /**
 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];
}

and then in a zoomHandler class we had this

-(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);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文