视图的框架是超视图坐标系中与轴对齐的矩形。框架完全包围视图的边界。如果视图被转换,框架会调整以紧密包围视图的新边界。
当您将Y轴旋转和透视应用于视图时,视图的左右边缘会移向其锚点(通常为视图的中心)。左边缘也可以长或短,而右边缘则相反。
因此,视图的框架(应用转换后)将为您提供转换视图的左边缘坐标和宽度,以及较高边的顶部和高度(可能是左边或右边)。这里是我的测试代码:
NSLog(@"frame before tilting = %@", NSStringFromCGRect(self.tiltView.frame));
float angle = 30.0;
CATransform3D rotationAndPerspectiveTransform = CATransform3DIdentity;
rotationAndPerspectiveTransform.m34 = -1.0/2000; // Perspective
rotationAndPerspectiveTransform = CATransform3DRotate(rotationAndPerspectiveTransform,
1 * angle/(180.0/M_PI), 0.0f, 1.0f, 0.0f);
self.tiltView.layer.transform = rotationAndPerspectiveTransform;
NSLog(@"frame after tilting = %@", NSStringFromCGRect(self.tiltView.frame));
下面是输出:
2012-01-04 12:44:08.405 layer[72495:f803] frame before tilting = {{50, 50}, {220, 360}}
2012-01-04 12:44:08.406 layer[72495:f803] frame after tilting = {{62.0434, 44.91}, {190.67, 370.18}}
您也可以获取视图的角的坐标,在上海华的使用convertPoint:fromView:
或convertPoint:toView:
坐标空间。测试代码:
CGRect bounds = self.tiltView.bounds;
CGPoint upperLeft = bounds.origin;
CGPoint upperRight = CGPointMake(CGRectGetMaxX(bounds), bounds.origin.y);
CGPoint lowerLeft = CGPointMake(bounds.origin.x, CGRectGetMaxY(bounds));
CGPoint lowerRight = CGPointMake(upperRight.x, lowerLeft.y);
#define LogPoint(P) NSLog(@"%s = %@ -> %@", #P, \
NSStringFromCGPoint(P), \
NSStringFromCGPoint([self.tiltView.superview convertPoint:P fromView:self.tiltView]))
LogPoint(upperLeft);
LogPoint(upperRight);
LogPoint(lowerLeft);
LogPoint(lowerRight);
输出:
2012-01-04 13:03:00.663 layer[72635:f803] upperLeft = {0, 0} -> {62.0434, 44.91}
2012-01-04 13:03:00.663 layer[72635:f803] upperRight = {220, 0} -> {252.713, 54.8175}
2012-01-04 13:03:00.663 layer[72635:f803] lowerLeft = {0, 360} -> {62.0434, 415.09}
2012-01-04 13:03:00.663 layer[72635:f803] lowerRight = {220, 360} -> {252.713, 405.182}
注意的Y upperLeft和upperRight点的坐标是上海华盈的坐标系中的不同。
感谢罗布 - 你是一位绝对的明星。花了几分钟让它在我的场景中工作,但你的解释是现货,所以让我在那里。 – Phil 2012-01-05 13:46:46