get rotation direction of UIView on touchesMoved

Posted by mlecho on Stack Overflow See other posts from Stack Overflow or by mlecho
Published on 2010-03-19T01:42:03Z Indexed on 2010/03/19 1:51 UTC
Read the original article Hit count: 565

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.

© Stack Overflow or respective owner

Related posts about objective-c

Related posts about uiview