这是一个棘手的问题。参考UIView关于帧属性的文档,它指出:
警告:如果transform属性不是标识转换,则此属性的值是未定义的,因此应该忽略。
所以诀窍是找到一个解决方法,它取决于你确切的需要。如果你只需要一个近似值,或者如果你的旋转总是90度的倍数,那么CGRectApplyAffineTransform()函数可能工作得很好。将它传递给感兴趣的UIButton的(未转换的)框架,以及按钮的当前转换,它会给你一个转换矩形。请注意,由于rect被定义为原点,宽度和高度,因此无法定义边长不平行于屏幕边的矩形。在不平行的情况下,它将返回旋转矩形的最小可能的边界矩形。
现在,如果你需要知道一个或全部转化点的精确坐标,我已经写代码之前计算它们,但它是一个有点更复杂:
- (void)computeCornersOfTransformedView:(UIView*)transformedView relativeToView:(UIView*)parentView {
/* Computes the coordinates of each corner of transformedView in the coordinate system
* of parentView. Each is corner represented by an independent CGPoint. Doesn't do anything
* with the transformed points because this is, after all, just an example.
*/
// Cache the current transform, and restore the view to a normal position and size.
CGAffineTransform cachedTransform = transformedView.transform;
transformedView.transform = CGAffineTransformIdentity;
// Note each of the (untransformed) points of interest.
CGPoint topLeft = CGPointMake(0, 0);
CGPoint bottomLeft = CGPointMake(0, transformedView.frame.size.height);
CGPoint bottomRight = CGPointMake(transformedView.frame.size.width, transformedView.frame.size.height);
CGPoint topRight = CGPointMake(transformedView.frame.size.width, 0);
// Re-apply the transform.
transformedView.transform = cachedTransform;
// Use handy built-in UIView methods to convert the points.
topLeft = [transformedView convertPoint:topLeft toView:parentView];
bottomLeft = [transformedView convertPoint:bottomLeft toView:parentView];
bottomRight = [transformedView convertPoint:bottomRight toView:parentView];
topRight = [transformedView convertPoint:topRight toView:parentView];
// Do something with the newly acquired points.
}
请原谅任何轻微代码中的错误,我写在浏览器中。不是最有用的IDE ...
这很令人惊讶,你可以编写像IN IN BROWSER这样的代码。哇!我不会越过这个“空白”,因为我忘记了如何拼写,并且必须从另一个项目中复制和粘贴。 – Fattie 2011-01-13 08:04:31