OGeek|极客世界-中国程序员成长平台

标题: ios - UIScrollView -zoomToRect :animated: and CGPoint conversion between views [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 16:39
标题: ios - UIScrollView -zoomToRect :animated: and CGPoint conversion between views

我有一个 UIScrollView A 和一个 subview B,(它本身就是 contentView D 的 subview A 的 strong> - 所以 BNOT ScrollView A) 的 contentView,它有一个 subview ,我们称之为C

+------------------------+
| A                      |
|                        |
|  +-------------------+ |
|  |D (=contentView)   | |
|  |  +-----------+    | |
|  |  | B         |    | |
|  |  |   +--+    |    | |
|  |  |   |C |    |    | |
|  |  |   +--+    |    | |
|  |  +-----------+    | |
|  +-------------------+ |
+------------------------+

我希望 scrollView 能够精确缩放,以便 C 填满我的 iPhone 屏幕,或者至少尽可能大地向我显示它。 我无法理解缩放之前 ScrollView 的 zoomScale 是如何进行的(假设我在 ScrollView 已经放大并且 zoomScale 为 2.45 时放大)。 那么第一个问题是,zoomScale 会影响 UIScrollView 放大到的矩形吗?

我的第二个问题是关于 CGPoint 转换的。 iOS 有一些内置函数可以在 View 之间转换 CGPoints/CGRects,但我根本无法从给定的引用中产生任何有效的东西。

这是我打算做的:

1) 将C的原点转换为A的坐标系:

CGPoint originConvertedToAsCoordinateSystem = [self convertPoint:C.frame.origin toView:A];

2) 缩放到 C 给定原点的矩形:

//shortened for the purpose of readability
CGPoint p = originConvertedToAsCoordinateSystem;

CGRect zoomRect = CGRectMake(p.x,p.y,C.frame.size.width,C.frame.size.height);
[A zoomToRect:zoomRect animated:YES];

在我给定的示例中,我忽略了 AzoomScale 的值,因为我真的不知道是乘还是除该值。



Best Answer-推荐答案


问题更新后更新:

如果您想以编程方式缩放和滚动 ScrollView ,以便 subview C 填充整个 ScrollView 矩形( ScrollView 边界),您需要在 View Controller 中执行以下操作:

CGRect rectToZoom = [self.D convertRect:self.C.frame fromView:self.B];

[self.scrollView zoomToRect:rectToZoom animated:YES];

这是假设您返回 View D 作为要缩放的 View :

- (UIView *)viewForZoomingInScrollViewUIScrollView *)scrollView
{
    return self.D;
}

ScrollView 的maximumZoomScale、contentSize和contentInset属性必须足够大以允许滚动和缩放(检查:尝试手动执行您想要的操作,看看是否可行)。

关于ios - UIScrollView -zoomToRect :animated: and CGPoint conversion between views,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20130474/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://sqlite.in/) Powered by Discuz! X3.4