如何使 UIScrollView 缩放到 UIImage 的大小?

发布于 2024-08-05 22:47:31 字数 397 浏览 2 评论 0原文

现在,当我双击 UIScrollView 时,我运行此代码(取自 Apple 自己的示例代码):

#define ZOOM_STEP 1.5
float newScale = [imageScrollView zoomScale] * ZOOM_STEP;
CGRect zoomRect = [self zoomRectForScale:newScale withCenter:tapPoint];
[imageScrollView zoomToRect:zoomRect animated:YES];

但是,我已将最大缩放设置为 3,但我想弄清楚如何使双击缩放为 1px = 1px,我的意思是,UIScrollView 放大到其中 UIImage 的实际大小。但我不知道如何计算图像有多大才能将其用于缩放?

Right now, when I double tap the UIScrollView I run this code (taken from Apple's own example code):

#define ZOOM_STEP 1.5
float newScale = [imageScrollView zoomScale] * ZOOM_STEP;
CGRect zoomRect = [self zoomRectForScale:newScale withCenter:tapPoint];
[imageScrollView zoomToRect:zoomRect animated:YES];

However, I have set the maximum zoom to 3, but I'd like to figure out how to make a double tap zoom to 1px = 1px, by that I mean, the UIScrollView zooms in to the actual size of the UIImage inside it. But I am not sure how to calculate how big the image in order to use it for zoom scaling?

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

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

发布评论

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

评论(1

羁绊已千年 2024-08-12 22:47:31

我找到了您从中提取的示例代码。它是 ScrollViewSuite 中的 TapToZoom 项目,可在 http://developer.apple.com/iphone 上找到。

我将计算 newScale 的行更改为滚动视图框架的大小与图像视图图像的大小之间的比率。这会产生您想要的放大效果,直到图像以 1:1 的分辨率显示。

注意:我还确保名为 imageView 的 UIImageView 是一个类变量。在原始示例代码中,它是在 viewDidLoad: 方法中本地声明的。

这里需要注意的是,滚动视图和图像具有相同的 x/y 比率。我只计算了宽度。

- (void)tapDetectingImageView:(TapDetectingImageView *)view gotDoubleTapAtPoint:(CGPoint)tapPoint {
    // double tap zooms in on them image to a 1:1 resolution (based on width)
    float newScale =  imageView.image.size.width / imageScrollView.frame.size.width;
    CGRect zoomRect = [self zoomRectForScale:newScale withCenter:tapPoint];
    [imageScrollView zoomToRect:zoomRect animated:YES];
}

I tracked down the sample code you were pulling from. It was the TapToZoom project in the ScrollViewSuite available at http://developer.apple.com/iphone.

I changed the line that calculates the newScale to be the ratio between the size of Scroll View's frame, and the size of the Image View's Image. This has the effect you desire of zooming-in until the image is shown at 1:1 resolution.

Note: I also had make sure the UIImageView named imageView was a class variable. In the original sample code, it was declared locally in the viewDidLoad: method.

The caveat here is that the scroll view and the image have the same x/y ratio. I ony did math on the width.

- (void)tapDetectingImageView:(TapDetectingImageView *)view gotDoubleTapAtPoint:(CGPoint)tapPoint {
    // double tap zooms in on them image to a 1:1 resolution (based on width)
    float newScale =  imageView.image.size.width / imageScrollView.frame.size.width;
    CGRect zoomRect = [self zoomRectForScale:newScale withCenter:tapPoint];
    [imageScrollView zoomToRect:zoomRect animated:YES];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文