iphone - 带有旋转的 scrollView 中 imageView 的边界

标签 iphone objective-c ios frame bounds

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

附言。有关更多详细代码,请在 Center of bounds 中找到

NSLog 调用 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 帧原点在旋转之前(NSLog in willRotateToInterfaceOrientation:duration)是(0, 0),在旋转之后(NSLog in willAnimateRotationToInterfaceOrientation:duration:),我认为它应该是(80, -120)。但它是 (80.000008, 0.000017);

最佳答案

frame 属性总是根据 boundscenter 返回一个计算值转换 UIView 的 属性。

根据 Apple 的 UIView documentation :

If the transform property is not the identity transform, the value of the frame property is undefined and therefore should be ignored.

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

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

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

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

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

换句话说,frame 属性给您意外值的原因是因为您的 View 应用了一个transform,渲染了frame 属性无意义。

目前尚不清楚您实际想要实现的目标,但同一 Apple 文档中的另一条注释可能会有所帮助:

If the transform property contains a non-identity transform, the value of the frame property is undefined and should not be modified. In that case, you can reposition the view using the center property and adjust the size using the bounds property instead.

关于iphone - 带有旋转的 scrollView 中 imageView 的边界,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10560937/

相关文章:

iphone - NSLayoutConstraints 代码使 View 居中并保持其纵横比

ios - NSPredicate 用于检测属性的类

iphone - 如何将字符串转换为带分隔符的数组?

ios - 在框架内将 Swift 文件公开给 Objective-C

iPhone - 未调用 webViewDidFinishLoad

iphone - UITableView 怪异!带图片

ios - 尝试?传播而不是返回 nil

ios - 在 Swift 单元测试中向下转换 NSManagedObjects 时出现命名空间问题

ios - 如何在特定日期前 2 天触发 UserNotifications

iphone - PurgeIdleCellConnections 导致应用程序卡住然后崩溃