【问题标题】:Bounds of an imageView in a scrollView with rotation带有旋转的滚动视图中的图像视图的边界
【发布时间】:2012-05-12 03:52:18
【问题描述】:

我有一个全屏 UIScrollView 来显示我的图像,它使用来自苹果示例代码的 PhotoScroller.app 中的一些代码。我对带有旋转的滚动视图中的图像视图的边界和转换点有点困惑。任何帮助将不胜感激!

ps。更多详细代码请查看Center of bounds

NSLog in call willRotateToInterfaceOrientation:duration:

2012-05-12 11:35:45.666 imageScrollView frame X and Y are 0.000000 and 0.000000 
2012-05-12 11:35:45.672 imageScrollView frame Width and Height are 320.000000 and 480.000000 
2012-05-12 11:35:45.677 imageScrollView bounds origin X and Y are 0.000000 and 0.000000 
2012-05-12 11:35:45.682 imageScrollView bounds Width and Height are 320.000000 and 480.000000 
2012-05-12 11:35:45.686 imageView frame origin X and Y are 0.000005 and 0.374437
2012-05-12 11:35:45.689 imageView frame size Width and Height are 320.000000 and 479.251129
2012-05-12 11:35:45.693 imageView bounds origin X and Y are 0.000000 and 0.000000
2012-05-12 11:35:45.697 imageView bounds size Width and Height are 641.000000 and 959.999939
2012-05-12 11:35:45.701 boundsCenter X and Y are 160.000000 and 240.000000 
2012-05-12 11:35:45.705 convertPoint origin X and Y are 320.500000 and 479.999969

NSLog 调用 willAnimateRotationToInterfaceOrientation:duration:

2012-05-12 11:36:05.975 imageScrollView frame X and Y are 0.000000 and 0.000000 
2012-05-12 11:36:05.978 imageScrollView frame Width and Height are 480.000000 and 320.000000 
2012-05-12 11:36:05.983 imageScrollView bounds origin X and Y are 0.000000 and 0.000000 
2012-05-12 11:36:05.987 imageScrollView bounds Width and Height are 480.000000 and 320.000000 
2012-05-12 11:36:05.990 imageView frame origin X and Y are 80.000008 and 0.000017
//I am confused here. Why is the X is 80.000008, and Y is 0.000017, but not (80, -120)?
2012-05-12 11:36:05.994 imageView frame size Width and Height are 320.000000 and 479.251099
2012-05-12 11:36:06.002 imageView bounds origin X and Y are 0.000000 and 0.000000
2012-05-12 11:36:06.006 imageView bounds size Width and Height are 641.000000 and 959.999878
2012-05-12 11:36:06.009 boundsCenter X and Y are 240.000000 and 160.000000 
2012-05-12 11:36:06.014 convertPoint origin X and Y are 320.500000 and 320.499969

关于自动调整大小配置:

actionSheetCoverView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
imageScrollView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
imageView.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;

旋转前的 imageview 帧原点(willRotateToInterfaceOrientation:duration 中的 NSLog)为 (0, 0),旋转后(willAnimateRotationToInterfaceOrientation:duration: 中的 NSLog),我认为应该是 (80, -120)。但它是 (80.000008, 0.000017);

【问题讨论】:

  • 你没有说你对什么感到困惑,你也没有说你为什么想要得到边界的中心,所以在这里真的很难帮助你。
  • @MichaelFrederick 我已经更新了帖子,请检查。
  • 用scrollView内容大小旋转后有问题吗?
  • @WarifAkhandRishi 内容大小为image.size,旋转后从未改变。

标签: iphone objective-c ios frame bounds


【解决方案1】:

frame 属性始终返回基于 UIView 的 boundscentertransform 属性的计算值。

根据苹果的UIView documentation

如果transform 属性不是恒等变换,则frame 属性的值是未定义的,因此应该被忽略。

问题是您在旋转动画开始时读取frame 属性,这意味着您的视图将具有非标识transform 值,使其frame 值毫无意义。

您可以根据other question 中的代码记录转换值:

NSLog(@"imageScrollView transform %@", NSStringFromCGAffineTransform(self.transform));

NSLog(@"imageView transform %@", NSStringFromCGAffineTransform(imageView.transform));

这将显示旋转时的变换属性。如果您不熟悉身份转换,请使用 [1, 0, 0, 1, 0, 0]

换句话说,frame 属性给您意外值的原因是因为您的视图应用了transform,从而使frame 属性变得毫无意义。

目前尚不清楚您实际上想要实现什么,但同一 Apple 文档中的其他注释可能会有所帮助:

如果transform 属性包含非身份转换,则frame 属性的值未定义且不应修改。在这种情况下,您可以使用center 属性重新定位视图并使用bounds 属性调整大小。

【讨论】:

  • 2012-05-19 16:47:27.495 imageScrollView 变换 [1, 0, 0, 1, 0, 0] 2012-05-19 16:47:27.499 imageView 变换 [0.49922, 0, 0, 0.49922, 0, 0] 。它也是恒等变换,但比例为 0.49922 对吧?
  • 对,这解释了您所看到的帧值,以及来自 convertPoint:toView 的值:(它返回图像视图的原始非转换坐标中的一个点)。
  • 所以渲染 frame 属性并不是没有意义的。对?那为什么 imageView 的帧原点是(80.000008, 0.000017)?我认为 origin.y 应该是负数。旋转应该围绕屏幕中心,对吧?
  • 文档说它没有意义,你应该使用 'center' 属性,但在你的情况下,值的来源很明显。 UIScrollView 负责设置其子视图的原点(或中心)。您应该重写 [UIScrollView layoutSubviews] 以查看它如何以及何时定位其子视图,如果需要,您可以使用该方法手动重新定位 ImageView。请注意,只要发生任何滚动,就会调用此方法。
  • 谢谢你的回答和cmets,真的很有帮助:)
【解决方案2】:

正如我所解释的,如果您不说出自己感到困惑的原因或您正在尝试做什么,就很难为您提供帮助。

要获取 imageView 的实际中心,可以使用:

imageView.center

【讨论】:

  • 我在这里很困惑。为什么 imageView 帧原点是 (80.000008, 0.000017),而不是 (80, -120)?
猜你喜欢
  • 2019-07-28
  • 1970-01-01
  • 1970-01-01
  • 2019-01-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-01-05
  • 1970-01-01
相关资源
最近更新 更多