gpt4 book ai didi

ios - 旋转后 UIView 坐标被交换但 UIWindow 不是?

转载 作者:技术小花猫 更新时间:2023-10-29 10:06:31 24 4
gpt4 key购买 nike

使用 Xcode 4.2.1 iPad iOS 5.0.1,创建一个新的“Single View”iPad 项目。在 Controller 中,添加:

- (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] ;
}

运行它,旋转设备,你会得到:

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 仍声称处于纵向模式...

此外,UIView 的大小似乎有点不对:本应为 20 的 y 坐标为 0 ...

谁知道这是什么意思?

最佳答案

UIWindow 的坐标系总是纵向的。它通过设置其 rootViewController 的 View 变换来应用旋转。例如,我使用单 View 模板创建了一个测试应用程序并运行了它。纵向:

(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 度旋转矩阵)。

关于您关于 UIView 大小的问题: View 的边界在其自己的坐标空间中,默认情况下, View 边界的原点位于其坐标空间的原点 (0,0)。 View 的框架位于其父级的坐标空间中。你可以在上面的递归描述中看到你期望的 20,或者直接打印框架:

(gdb) p (CGRect)[[[[(id)UIApp keyWindow] subviews] lastObject] frame]
$2 = {
origin = {
x = 0,
y = 20
},
size = {
width = 768,
height = 1004
}
}

关于ios - 旋转后 UIView 坐标被交换但 UIWindow 不是?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8598315/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com