ios 获取 UIView 的当前角度/旋转/弧度?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5754877/
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 19:40:47  来源:igfitidea点击:

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

ioscocoa-touchuiviewrotation

提问by Hjalmar

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

你如何获得 UIView 的当前角度/旋转/弧度?

回答by Krishnabhadra

You can do it this way...

你可以这样做...

CGFloat radians = atan2f(yourView.transform.b, yourView.transform.a); 
CGFloat degrees = radians * (180 / M_PI);

回答by Peter Kreinz

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

回答by Rakesh Yembaram

For Swift 3, you could use the following code:

对于 Swift 3,您可以使用以下代码:

let radians:Float = atan2f(Float(view.transform.b), Float(view.transform.a))
let degrees:Float = radians * Float(180 / M_PI)

回答by Dan Loewenherz

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

很多其他答案都参考了atan2f,但是鉴于我们正在对CGFloats 进行操作,我们可以使用atan2并跳过不必要的中间转换:

Swift 4:

斯威夫特 4:

let radians = atan2(yourView.transform.b, yourView.transform.a)
let degrees = radians * 180 / .pi

回答by Rohit Sisodia

//For Swift 3: M_PI is depreciated now Use Double.pi

//对于 Swift 3: M_PI 现在折旧使用 Double.pi

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

//For Swift 4:

//对于Swift 4:

let radians = atan2(yourView.transform.b, yourView.transform.a)
let degrees = radians * 180 / .pi

回答by Sujay U N

In swift 2 and Xcode 7 :

在 swift 2 和 Xcode 7 中:

let RdnVal = CGFloat(atan2f(Float(NamVyu.transform.b), Float(NamVyu.transform.a)))
let DgrVal = RdnVal * CGFloat(180 / M_PI)