旋转后,会交换UIView坐标,但不会交换UIWindow吗? [英] After rotation UIView coordinates are swapped but UIWindow's are not?
问题描述
使用Xcode 4.2.1 iPad iOS 5.0.1,创建一个新的"Single View" iPad项目.在控制器中,添加:
Using Xcode 4.2.1 iPad iOS 5.0.1, create a new "Single View" iPad project. In the controller, add:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return YES;
}
- (void) dumpView: (UIView *) view {
CGRect frame = view.frame ;
CGRect bounds = view.bounds ;
CGPoint center = view.center ;
NSLog(@"view [%@]:%d frame=%@ bounds=%@ center=%@"
, NSStringFromClass(view.class)
, [view hash]
, NSStringFromCGRect(frame)
, NSStringFromCGRect(bounds)
, NSStringFromCGPoint(center)) ;
}
- (void) didRotateFromInterfaceOrientation:(UIInterfaceOrientation) fromInterfaceOrientation {
NSLog(@"INViewController.didRotateFromInterfaceOrientation: %d to %d", fromInterfaceOrientation, self.interfaceOrientation) ;
[self dumpView: self.view] ;
[self dumpView: self.view.superview] ;
}
运行它,旋转设备,您将得到:
Run it, rotate the device and you will get:
INViewController.didRotateFromInterfaceOrientation: 2 to 4
view [UIView] bounds={{0, 0}, {1024, 748}} center={394, 512}
view [UIWindow] bounds={{0, 0}, {768, 1024}} center={384, 512}
换句话说,UIView的坐标已按预期转换为横向",但是其父级UIWindow仍然声称处于纵向模式...
In other words, the UIView has its coordinates "swapped to landscape" as expected, but its parent UIWindow still claims to be in portrait mode...
此外,UIView的大小似乎略有错误:应为20的y坐标为0 ...
Also, the UIView size seems to be slightly wrong: the y coordinate which should be at 20 is at 0 ...
有人知道这意味着什么吗?
Anyone knows what this means?
推荐答案
UIWindow的坐标系始终为纵向.它通过设置其rootViewController的视图的变换来应用旋转.例如,我使用单视图模板创建了一个测试应用程序并运行了它.纵向放置:
The UIWindow's coordinate system is always in portrait orientation. It applies the rotation by setting its rootViewController's view's transform. For example, I created a test app using the single-view template and ran it. In portrait orientation:
(gdb) po [[(id)UIApp keyWindow] recursiveDescription]
<UIWindow: 0x9626a90; frame = (0 0; 768 1024); layer = <UIWindowLayer: 0x9626b80>>
| <UIView: 0x96290e0; frame = (0 20; 768 1004); autoresize = W+H; layer = <CALayer: 0x96286a0>>
向左旋转后:
(gdb) po [[(id)UIApp keyWindow] recursiveDescription]
<UIWindow: 0x9626a90; frame = (0 0; 768 1024); layer = <UIWindowLayer: 0x9626b80>>
| <UIView: 0x96290e0; frame = (0 0; 748 1024); transform = [0, 1, -1, 0, 0, 0]; autoresize = W+H; layer = <CALayer: 0x96286a0>>
向右旋转后:
(gdb) po [[(id)UIApp keyWindow] recursiveDescription]
<UIWindow: 0x9626a90; frame = (0 0; 768 1024); layer = <UIWindowLayer: 0x9626b80>>
| <UIView: 0x96290e0; frame = (20 0; 748 1024); transform = [0, -1, 1, 0, 0, 0]; autoresize = W+H; layer = <CALayer: 0x96286a0>>
请注意在旋转设备时如何设置变换(设置为90度旋转矩阵).
Notice how the transform is set (to a 90-degree rotation matrix) when the device is rotated.
关于UIView大小的问题:视图的边界在其自己的坐标空间中,默认情况下,视图的边界的原点在其坐标空间的原点(0,0).视图的框架位于其父级的坐标空间中.您可以在上面的递归描述中看到期望的20,或者直接打印框架:
Regarding your question about the UIView size: a view's bounds are in its own coordinate space, and by default a view's bounds' origin is at the origin (0,0) of its coordinate space. A view's frame is in its parent's coordinate space. You can see the 20 you were expecting in the recursive descriptions above, or by printing the frame directly:
(gdb) p (CGRect)[[[[(id)UIApp keyWindow] subviews] lastObject] frame]
$2 = {
origin = {
x = 0,
y = 20
},
size = {
width = 768,
height = 1004
}
}
这篇关于旋转后,会交换UIView坐标,但不会交换UIWindow吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!