Search code examples
objective-ciostrigonometrycatransform3d

Rotating line like a balance board


Well,
i have a circle like this in the photo.
I want to rotate my red line to the degree that i want.

The circle start from 0 to 300 degrees.

I started to do something with

CGFloat wAngle = Degrees2Radians([_Weight.text intValue]/300.0*360);
_Arrow.layer.transform = CATransform3DMakeRotation (wAngle + M_PI, 0, 0, 1);

but in this snippet, 0 value was on top, not on bottom.
Probably because i'm not a genius in trigonometry... :)

enter image description here

What is the correct way to rotate properly the arrow?
How are the values of angle to set?

thanks.


Solution

  • You don't need to transform degrees and radians. You have a relative value:

    CGFloat relativeAngle = [_Weight.text intValue] / 300.0;
    

    So just use it:

    _Arrow.layer.transform = CATransform3DMakeRotation(relativeAngle * M_PI*2, 0, 0, 1);
    

    If the start is wrong just change the initial position of your view. (Or sum up the wrong angle to the new angle. Like (relativeAngle * M_Pi*2 + correction).

    // what PI means in degrees
    M_PI * 2 = 360°  
    M_PI     = 180°  
    M_PI_2   = 90°  
    M_PI_4   = 45°