Obtener el ángulo/rotación/radián actual para un UIView?


¿Cómo se obtiene el ángulo/rotación/radián actual que tiene un UIView?

Author: Dan Loewenherz, 2011-04-22

6 answers

Puedes hacerlo de esta manera...

CGFloat radians = atan2f(yourView.transform.b, yourView.transform.a); 
CGFloat degrees = radians * (180 / M_PI);
 102
Author: Krishnabhadra,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-12-18 09:51:22

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) )
 12
Author: Peter Kreinz,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-01-13 11:55:27

Para Swift 3, puede usar el siguiente código:

let radians:Float = atan2f(Float(view.transform.b), Float(view.transform.a))
let degrees:Float = radians * Float(180 / M_PI)
 6
Author: Rakesh Yembaram,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-04-17 09:17:40

En swift 2 y Xcode 7:

let RdnVal = CGFloat(atan2f(Float(NamVyu.transform.b), Float(NamVyu.transform.a)))
let DgrVal = RdnVal * CGFloat(180 / M_PI)
 2
Author: Sujay U N,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-07-21 02:20:15

Muchas de las otras respuestas hacen referencia a atan2f, pero dado que estamos operando con CGFloats, podemos usar atan2 y saltarnos el molde intermedio innecesario:

Swift 4:

let radians = atan2(yourView.transform.b, yourView.transform.a)
let degrees = radians * 180 / .pi
 2
Author: Dan Loewenherz,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-01-02 17:13:25

//Para Swift 3: M_PI se deprecia ahora Usa Double.pi

let radians = atan2f(Float(yourView.transform.b), Float(yourView.transform.a));
let degrees = radians * Float(180 / Double.pi)

/ / Para Swift 4:

let radians = atan2(yourView.transform.b, yourView.transform.a)
let degrees = radians * 180 / .pi
 2
Author: Rohit Sisodia,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-01-08 10:28:38