如何使 UIScrollView 缩放到 UIImage 的大小?
现在,当我双击 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我找到了您从中提取的示例代码。它是 ScrollViewSuite 中的 TapToZoom 项目,可在 http://developer.apple.com/iphone 上找到。
我将计算 newScale 的行更改为滚动视图框架的大小与图像视图图像的大小之间的比率。这会产生您想要的放大效果,直到图像以 1:1 的分辨率显示。
注意:我还确保名为 imageView 的 UIImageView 是一个类变量。在原始示例代码中,它是在 viewDidLoad: 方法中本地声明的。
这里需要注意的是,滚动视图和图像具有相同的 x/y 比率。我只计算了宽度。
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.