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

前端 未结 6 1316
傲寒
傲寒 2021-01-30 12:40

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

相关标签:
6条回答
  • 2021-01-30 13:13

    For Swift 3, you could use the following code:

    let radians:Float = atan2f(Float(view.transform.b), Float(view.transform.a))
    let degrees:Float = radians * Float(180 / M_PI)
    
    0 讨论(0)
  • 2021-01-30 13:16

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

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

    //For Swift 4:

    let radians = atan2(yourView.transform.b, yourView.transform.a)
    let degrees = radians * 180 / .pi
    
    0 讨论(0)
  • 2021-01-30 13:17

    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
    
    0 讨论(0)
  • 2021-01-30 13:28

    You can do it this way...

    CGFloat radians = atan2f(yourView.transform.b, yourView.transform.a); 
    CGFloat degrees = radians * (180 / M_PI);
    
    0 讨论(0)
  • 2021-01-30 13:28

    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) )
    
    0 讨论(0)
  • 2021-01-30 13:31

    In swift 2 and Xcode 7 :

    let RdnVal = CGFloat(atan2f(Float(NamVyu.transform.b), Float(NamVyu.transform.a)))
    let DgrVal = RdnVal * CGFloat(180 / M_PI)
    
    0 讨论(0)
提交回复
热议问题