Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/37.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
iPhone中UIView帧旋转后的怪异行为_Iphone_Uikit_Frame_Cgaffinetransform - Fatal编程技术网

iPhone中UIView帧旋转后的怪异行为

iPhone中UIView帧旋转后的怪异行为,iphone,uikit,frame,cgaffinetransform,Iphone,Uikit,Frame,Cgaffinetransform,创建一个方形UIView对象testView,并将其添加到viewDidLoad: - (void)viewDidLoad { [super viewDidLoad]; CGRect initialRect = testView_.frame; NSLog(@"before rotation: w %f h %f x %f y %f", initialRect.size.width, initialRect.size.height, initialRect.origin.x, initialRe

创建一个方形UIView对象testView,并将其添加到viewDidLoad:

- (void)viewDidLoad {
[super viewDidLoad];
CGRect initialRect = testView_.frame;
NSLog(@"before rotation: w %f h %f x %f y %f", initialRect.size.width, initialRect.size.height, initialRect.origin.x, initialRect.origin.y);
testView_.transform = CGAffineTransformMakeRotation(0.1);
NSLog(@"after rotation: w %f, h %f, x %f, y %f", testView_.frame.size.width, testView_.frame.size.height, testView_.frame.origin.x, testView_.frame.origin.y);
testView_.frame = initialRect;
NSLog(@"reassign: w %f, h %f, x %f, y %f", testView_.frame.size.width, testView_.frame.size.height, testView_.frame.origin.x, testView_.frame.origin.y);
}
我在控制台中收到此消息:

2011-04-27 12:30:32.492 Test[31890:207] before rotation: w 100.000000 h 100.000000 x 20.000000 y 20.000000
2011-04-27 12:30:32.494 Test[31890:207] after rotation: w 109.483757, h 109.483757, x 15.258121, y 15.258121
2011-04-27 12:30:32.495 Test[31890:207] reassign: w 117.873589, h 100.000000, x 11.063205, y 20.000000

我无法理解帧值变化背后的逻辑,尤其是最后一个。谁能启发我?谢谢。

在UIView类参考中,您可以看到,您不应该使用与CGAffineTransformity不同的transform属性设置视图的框架


如果要更改变换视图的位置,应使用居中属性。如果要调整大小,应使用边界属性

我不能完全肯定这个答案。所以我把它作为一个注释,当我们旋转一个视图,比如说B,它是a的一个子视图,只有B的变换被改变。框架是根据父视图中的位置来考虑的,即B在A中的位置。因此,当我们旋转B时,A中的总平方空间(在A的变换中计算)B增加。这就是第二个日志的原因。现在第三个日志强制将原始帧赋予B,但旋转的B现在的大小比原始大小大,这迫使parentView A通过保持一个方向(y和高度/x和宽度)不变来自动调整B。问得好。我也想知道。你没有得到答案…太糟糕了。如果这对任何人都有帮助,我首先设置
view.transform=CGAffineTransformIdentity
,然后设置帧,然后使用
view.transform=CGAffineTransformMakeRotation(角度)设置旋转,解决了这个问题@Liron这似乎不再有效了。框架有问题。你想做什么?这篇文章很老了,所以我不确定我是否真的记得这是怎么回事。如今,在大多数情况下,我发现使用自动布局比手动分配框架更容易,但这当然取决于应用程序。@Liron正在处理旧代码,而自动布局毫无意义。不管怎么说,在代码中,顺序是正确的,过去是有效的,但现在不行了。框架喜欢改变。