简体   繁体   English

iOS获取子视图的旋转

[英]iOS Get Subview's Rotation

I'm using CGAffineTransformMakeRotation to rotate a subview using its transform property. 我正在使用CGAffineTransformMakeRotation使用其transform属性旋转子视图。 Later, I need to find out how far the subview has been rotated. 以后,我需要找出子视图旋转了多远。 I realize that I could simply use an int to keep track of this, but is there a simple way to get the current rotation of the subview? 我意识到我可以简单地使用int来跟踪此情况,但是有没有一种简单的方法来获取子视图的当前旋转?

CGAffineTransformMakeRotation is explicitly defined to return a matrix with cos(angle) in the transform's a value, sin(angle) in b , etc (and, given the way the transform works, that's the only thing it really could do). CGAffineTransformMakeRotation明确定义为返回一个矩阵,其中变换的a值为cos(angle), b sin(angle)等等(并且,鉴于变换的工作方式,这是它唯一可以做的事情)。

Hence you can work out the current rotation by doing some simple inverse trigonometry. 因此,您可以通过执行一些简单的逆三角法来计算当前旋转。 atan2 is probably the thing to use, because it'll automatically figure out the quadrants appropriately. atan2可能是要使用的东西,因为它会自动找出合适的象限。

So, eg 所以,例如

- (float)currentAngleOfView:(UIView *)view
{
    CGAffineTransform transform = view.transform;
    return atan2f(transform.b, transform.a);
}

Because that'll do an arctangent that'll involve dividing the a and b fields in an appropriate manner, that method will continue to work even if you apply scaling (as long as it's the same to each axis) or translation. 因为这将产生一个反切线,将涉及以适当的方式划分ab字段,所以即使您应用缩放(只要每个轴都相同)或平移,该方法也将继续起作用。

If you want to apply more complex or arbitrary transformations then things get a lot more complicated. 如果要应用更复杂或任意的转换,则事情会变得更加复杂。 You'll want to look how to calculate normal matrices. 您将要看一下如何计算普通矩阵。 From memory I think you'd want the adjugate, which is about as much fun to work out as it sounds. 从记忆中,我认为您会需要这个陪审员,这听起来很有趣。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM