Calculate the rotation angle in a pie chart

I want to rotate the image around its center point. The problem I am facing is getting the angle to calculate in touch the moved event (I don't want to use multi touch). I am using current code

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{

NSArray *allTouches = [touches allObjects]; 
gestureStartPoint = gestureMovedPoint;//i am getting the gestureStartPoint on touch began event

gestureMovedPoint = [[allTouches objectAtIndex:0] locationInView:[self superview]];

NSLog(@"gestureMovedPoint = %@",NSStringFromCGPoint(gestureMovedPoint));

}

CGFloat previousAngle = [self angleBetweenPoints:gestureStartPoint second11:gestureMovedPoint]; // atan2(gestureMovedPoint.y - gestureStartPoint.y, gestureMovedPoint.x - gestureStartPoint.x) * 180 / M_PI;
CGFloat currentAngle =atan2(self.transform.b, self.transform.a);//atan2(gestureMovedPoint.y - gestureStartPoint.y,gestureMovedPoint.x - gestureStartPoint.x) * 180 / M_PI;



CGFloat angleToRotate = currentAngle - previousAngle;



float xpoint = (((atan2((gestureMovedPoint.x - gestureStartPoint.x) , (gestureMovedPoint.y - gestureStartPoint.y)))*180)/M_PI);

    CGAffineTransform transform = CGAffineTransformMakeRotation(angleToRotate-100);



self.transform = transform;

Please help me find a solution, as I am stuck here and should complete this application very soon, as there is a dead line. thanks in advance

+2
source share
3 answers

Glad I remember triginometry


-(void)degreesToRotateObjectWithPosition:(CGPoint)objPos andTouchPoint:(CGPoint)touchPoint{

   float dX = touchPoint.x-objPos.x;        // distance along X
   float dY = touchPoint.y-objPos.y;        // distance along Y
   float radians = atan2(dY, dX);          // tan = opp / adj

   //Now we have to convert radians to degrees:
   float degrees = radians*M_PI/360;

   return degrees;
}

Once you have a good method, just do it in the sensory event method. (I forgot what he called ...)

CGAffineTransform current = view.transform;

[view setTransform:CGAffineTransformRotate(current,
[self degreesTorotateObjectWithPosition:view.frame.origin
andTouchPoint:[touch locationInView:parentView]] //Note: parentView = The view that your object to rotate is sitting in.

, . , setTransform. . .

,

Aurum Aquila

+2

. . .

0

, , , , 100% , . , , :

- (CGFloat) pointToAngleFromCenter: (CGPoint) point {
    // transform point to a self.center'ed origin based coordinate system
    point.x = point.x - self.center.x ;
    // ditto for y, but compensate for y going downwards to y going upwards
    point.y = self.center.y - point.y ;
    return ::atan2(point.y, point.x) ;
}

- , .

What he does is that he takes a point in the coordinates of the parent view, redirects it relative to the center of the view (which is in the coordinate of the parent view) and calculates the angle between this entangled point and the [0X] axis. To do this, it normalizes y to normal mathematical coordinates (y rises when its value increases, not down), therefore self.center.y - point.y, and not vice versa.

Finally, in touchsMoved:

- (void) touchesMoved: (NSSet *) touches withEvent: (UIEvent *) event {
    UITouch * touch = [touches anyObject] ;

    CGFloat currA = [self pointToAngleFromCenter:[touch locationInView:self.superview]] ;
    CGFloat prevA = [self pointToAngleFromCenter:[touch previousLocationInView:self.superview]] ;
    // the current value of the rotation angle
    CGFloat tranA = ::atan2(self.transform.b, self.transform.a) ;
    // the angle difference between last touch and the current touch
    CGFloat diffA = currA - prevA ;
    // the new angle resulting from applying the difference
    CGFloat angle = tranA - diffA ;

    CGAffineTransform t = ::CGAffineTransformMakeRotation(angle) ;

    self.transform = t ;
    [self setNeedsDisplay] ;
}
0
source

All Articles