ios iphone sdk CGAffineTransform 获取对象的旋转角度

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2051811/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 16:47:07  来源:igfitidea点击:

iphone sdk CGAffineTransform getting the angle of rotation of an object

ioscocoa-touch

提问by nathanjosiah

how do i calculate the angle of rotation for any given object (ie a uiimageview)?

我如何计算任何给定对象(即 uiimageview)的旋转角度?

回答by kennytm

Technically you can't, because the transform can include a skewoperation which turns the image into a parallelogram and the rotation angle isn't defined anymore.

从技术上讲,您不能,因为变换可以包括将图像变成平行四边形的倾斜操作,并且不再定义旋转角度。

Anyway, since the rotation matrix generates

无论如何,由于旋转矩阵生成

 cos(x)  sin(x)   0
-sin(x)  cos(x)   0
   0        0     1

You can recover the angle with

你可以恢复角度

return atan2(transform.b, transform.a);

回答by sch

You can easily get the angle of the rotation like this:

您可以像这样轻松获得旋转角度:

CGFloat angle = [(NSNumber *)[view valueForKeyPath:@"layer.transform.rotation.z"] floatValue];


For example:

例如:

view.transform = CGAffineTransformMakeRotation(0.02);
CGFloat angle = [(NSNumber *)[view valueForKeyPath:@"layer.transform.rotation.z"] floatValue];
NSLog(@"%f", angle); // 0.020000


From the documentation:

文档

Core Animation extends the key-value coding protocol to allow getting and setting of the common values of a layer's CATransform3D matrix through key paths. Table 4 describes the key paths for which a layer's transform and sublayerTransform properties are key-value coding and observing compliant

Core Animation 扩展了键值编码协议,允许通过键路径获取和设置层的 CATransform3D 矩阵的公共值。表 4 描述了层的 transform 和 sublayerTransform 属性是键值编码和观察兼容的关键路径

回答by leon

Or you can use acos and asin functions. You will get exactly the same result:

或者您可以使用 acos 和 asin 函数。你会得到完全相同的结果:

 NSLog (@"%f %f %f", acos (MyView.transform.a), asin (MyView.transform.b), atan2(MyView.transform.b, MyView.transform.a) );

回答by Abhishek Bedi

You can try with this:

你可以试试这个:

- (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
{
    CGPoint newLocationPoint = [[touches anyObject] locationInView:self.superview];
    int x = self.center.x;
    int y = self.center.y;    
    float dx = newLocationPoint.x - x;
    float dy = newLocationPoint.y - y;    
    double angle = atan2(-dx,dy);  
    self.layer.position = self.center;
    self.layer.transform = CATransform3DMakeRotation(angle, 0, 0, 1); 
    NSLog(@"touchesMoved %f %f %d,%d   %f,%f  angle:%f",newLocationPoint.x,newLocationPoint.y,x,y,dx,dy,angle);    
}

回答by Rendel

I know this is very old question but, if anyone like me faces with this problem using CALayers and CATransform3D, you can get angle with:

我知道这是一个非常古老的问题,但是,如果像我这样的人使用 CALayer 和 CATransform3D 遇到这个问题,您可以通过以下方式获得角度:

extension CATransform3D {

    var xAxisAngle: CGFloat { get { return atan2(self.m23, self.m33) } }
    var yAxisAngle: CGFloat { get { return atan2(self.m31, self.m11) } }
    var zAxisAngle: CGFloat { get { return atan2(self.m12, self.m11) } }

}