在iPhone中旋转后UIView帧的奇怪行为 [英] Weird behavior of UIView frame after rotation in iPhone

查看:125
本文介绍了在iPhone中旋转后UIView帧的奇怪行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

创建一个方形UIView对象testView_,并在viewDidLoad中添加:

Create a square UIView object testView_, and add this at 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);
}

我在控制台收到此消息:

I receive this in the console:

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

我无法弄清楚这一变化背后的逻辑帧值,尤其是最后一帧。任何人都可以开导我吗?谢谢。

I couldn't figure out the logic behind this change in frame value, especially the last one. Can anyone enlighten me? Thanks.

推荐答案

在UIView类参考中你可以看到在其中不应设置视图的框架,其转换属性不同于 CGAffineTransformIdentity

In UIView Class Reference you can see here that you should not set the frame of a view with transform property different than CGAffineTransformIdentity.

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

If you want to change the position of the transformed view, you should use center property. If you want to adjust the size you should use bounds property.

这篇关于在iPhone中旋转后UIView帧的奇怪行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆