get rotation direction of UIView on touchesMoved
- by mlecho
this may sound funny, but i spent hours trying to recreate a knob with a realistic rotation using UIView and some trig. I achieved the goal, but now i can not figure out how to know if the knob is rotating left or right. The most pertinent part of the math is here:
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint pt = [touch locationInView:self];
float dx = pt.x - iv.center.x;
float dy = pt.y - iv.center.y;
float ang = atan2(dy,dx);
//do the rotation
if (deltaAngle == 0.0) {
deltaAngle = ang;
initialTransform = iv.transform;
}else
{
float angleDif = deltaAngle - ang;
CGAffineTransform newTrans = CGAffineTransformRotate(initialTransform, -angleDif);
iv.transform = newTrans;
currentValue = [self goodDegrees:radiansToDegrees(angleDif)];
}
}
ideally, i could leverage a numeric value to tell me if the rotation is positive or negative.