emil.c
emil.c

Reputation: 2018

How do I get the angle to which I want to rotate UIView

degrees

The red line is UIView that is rotating with anchor point at its center. What I want to achieve is that when I drag my finger around that green circle holding it on the red circle, that red line goes along with the finger. But I can't do that without knowing the angle. How do I count it?

Upvotes: 0

Views: 409

Answers (2)

Pablo
Pablo

Reputation: 613

You can use the Pythagorean theorem to calculate the radius. In this case you know that the center of the circle is at x:152, y:0. Your touch location would be at x1, y1, which gives you enough information to calculate the legs. In code:

CGPoint center = CGPointMake(152.0f, 0.0f);
CGPoint touchPoint = [gestureRecognizer locationInView:yourView];
float xLeg = fabs(center.x - touchPoint.x);
float yLeg = fabs(touchPoint.y);
angle = atan(xLeg / yLeg);
//float radius = sqrt(pow(xLeg, 2) + pow(yLeg, 2));

Hope it helps!

Note: Edited to reflect change of wording. Original question asked for radius.

Upvotes: 1

vikingosegundo
vikingosegundo

Reputation: 52227

use the arctangent of 3 points. (center, the point, where touch started/ or a fixed position that you define as angle 0 , the current touch point )

double AngleBetweenThreePoints(CGPoint point1,CGPoint point2, CGPoint point3)
{
    CGPoint p1 = CGPointMake(point2.x - point1.x, -1*point2.y - point1.y *-1);
    CGPoint p2 = CGPointMake(point3.x -point1.x, -1*point3.y -point1.y*-1);
    double angle = atan2(p2.x*p1.y-p1.x*p2.x,p1.y*p2.y);        
    return angle /(2* M_PI);
}

Upvotes: 1

Related Questions