在iPhone中旋转后UIView帧的奇怪行为

Enz*_*ran 17 iphone frame uikit 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, 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);
}
Run Code Online (Sandbox Code Playgroud)

我在控制台中收到了这个:

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
Run Code Online (Sandbox Code Playgroud)

我无法弄清楚帧值变化背后的逻辑,特别是最后一个.任何人都可以开导我吗?谢谢.

los*_*sic 36

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

如果要更改变换视图的位置,则应使用center属性.如果要调整大小,则应使用bounds属性.

  • 如果这对任何人有帮助,我首先设置`view.transform = CGAffineTransformIdentity`,然后设置框架,然后用`view.transform = CGAffineTransformMakeRotation(angle)设置旋转来解决这个问题; (4认同)