Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the current angle/rotation/radian for a UIView?

How do you get the current angle/rotation/radian a UIView has?

like image 879
Hjalmar Avatar asked Apr 22 '11 11:04

Hjalmar


3 Answers

You can do it this way...

CGFloat radians = atan2f(yourView.transform.b, yourView.transform.a); 
CGFloat degrees = radians * (180 / M_PI);
like image 100
Krishnabhadra Avatar answered Nov 15 '22 06:11

Krishnabhadra


Swift:

// Note the type reference as Swift is now string Strict

let radians:Double = atan2( Double(yourView.transform.b), Double(yourView.transform.a))
let degrees:CGFloat = radians * (CGFloat(180) / CGFloat(M_PI) )
like image 17
Peter Kreinz Avatar answered Nov 15 '22 05:11

Peter Kreinz


A lot of the other answers reference atan2f, but given that we're operating on CGFloats, we can just use atan2 and skip the unnecessary intermediate cast:

Swift 4:

let radians = atan2(yourView.transform.b, yourView.transform.a)
let degrees = radians * 180 / .pi
like image 7
dwlz Avatar answered Nov 15 '22 06:11

dwlz