32

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

Serenity
  • 35,289
  • 20
  • 120
  • 115
nathanjosiah
  • 4,441
  • 4
  • 35
  • 47

5 Answers5

91

Technically you can't, because the transform can include a skew operation 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);
kennytm
  • 510,854
  • 105
  • 1,084
  • 1,005
  • 1
    what is transform.b and transform.a? are b and a properties of an transform? have never seen those. – openfrog Feb 02 '10 at 17:44
  • 2
    @openfrog: http://developer.apple.com/iphone/library/documentation/GraphicsImaging/Reference/CGAffineTransform/Reference/reference.html#//apple_ref/doc/uid/TP30000946-CH2g-BBCJFFAE – kennytm Feb 02 '10 at 18:07
36

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

ide
  • 19,942
  • 5
  • 64
  • 106
sch
  • 27,436
  • 3
  • 68
  • 83
3

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

 NSLog (@"%f %f %f", acos (MyView.transform.a), asin (MyView.transform.b), atan2(MyView.transform.b, MyView.transform.a) );
leon
  • 1,412
  • 2
  • 17
  • 26
0

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);    
}
Pradhyuman sinh
  • 3,936
  • 1
  • 23
  • 38
Abhishek Bedi
  • 5,205
  • 2
  • 36
  • 62
0

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:

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) } }

}

Rendel
  • 1,794
  • 3
  • 13
  • 16