如何使用滚动视图缩放到中心? [英] How to zoom to the center with a scrollview?

查看:183
本文介绍了如何使用滚动视图缩放到中心?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

我们正在使用

(CGRect)zoomRectForScale :(浮动)缩放与中心:(CGPoint)中心

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

用于从我们用于点按缩放的手势识别器接受中心cgpoint的方法;但是,由于我们不再使用手势识别器,我们将不得不弄清楚传递它的cgpoint。此外,这种方法适用于水龙头缩放,所以我不认为这是我们遇到问题的地方。

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.

我们尝试这样做

    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,但它不起作用(它缩放到中心右侧)。

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?

推荐答案

我们得到了它的工作我想我会发布我们最终做的事情

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

然后在zoomHandler类中我们有了这个

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

这篇关于如何使用滚动视图缩放到中心?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆