1

I'm encountering some problems with rotating a uiview again. This time, im trying to rotate a uiview 1/12 the speed of another uiview I'm rotating at normal speed. However, when I try to accomplish this task, the uiview I'm trying to move slower moves like this:

1st Update https://www.youtube.com/watch?v=wj3nRJo5CMM&feature=youtu.be

2nd Update https://www.youtube.com/watch?v=YLRkUzXSDtQ&feature=youtu.be

Here's my code:

- (void)rotateHand:(UIPanGestureRecognizer *)panGesture {
    if ([(UIPanGestureRecognizer*)panGesture state] == UIGestureRecognizerStateBegan) {
        CGPoint touchPoint = [panGesture locationInView:[self view]];
        float dx = touchPoint.x - minHandContainer.center.x;
        float dy = touchPoint.y - minHandContainer.center.y;
        arcTanMin = atan2(dy,dx);
        arcTanHour = atan2(hourHand.center.x - minHandContainer.center.x, hourHand.center.y - minHandContainer.center.y);
        if (arcTanMin < 0) {
            arcTanMin = 2 * M_PI + arcTanMin;
        }
        if (arcTanHour < 0) {
            arcTanHour = 2 * M_PI + arcTanMin;
        }
        NSLog(@"arcTanMin %f", arcTanMin);
        startTransformMin = minHandContainer.transform;
        startTransformHour = hourHandContainer.transform;
    }
    if ([(UIPanGestureRecognizer*)panGesture state] == UIGestureRecognizerStateChanged) {
        CGPoint pt = [panGesture locationInView:[self view]];
        float dx = pt.x  - minHandContainer.center.x;
        float dy = pt.y  - minHandContainer.center.y;
        float ang = atan2(dy,dx);
        if (ang < 0) {
            ang = 2 * M_PI + ang;
        }
        float angleDifferenceM = arcTanMin - ang;
        float angleDifferenceH = arcTanHour + angleDifferenceM * (1.0/12.0);
        NSLog(@"angleDiffM %f", angleDifferenceM);
        NSLog(@"angleDiffH %f", angleDifferenceH);
        minHandContainer.transform = CGAffineTransformRotate(startTransformMin, -angleDifferenceM);
        hourHandContainer.transform = CGAffineTransformRotate(startTransformHour, -angleDifferenceH);
    }
}
Alexyuiop
  • 813
  • 2
  • 10
  • 25
  • Isn't it obvious? When the minute hand passes across the x-axis it's angle become negative, and the way you're relating the angle of the hour hand that makes it jump across the y axis like it did. It's not a glitch. I see you're trying to handle that case, but the math somewhere is wrong. – user2320861 Aug 16 '14 at 01:52
  • How should I fix it? – Alexyuiop Aug 16 '14 at 02:21

1 Answers1

1

It appears that you're using arcTanMin as the starting reference angle for both the minute hand and hour hand. So when you make the jump across the x-axis, both angleDifferenceM and angleDifferenceH are making the jump (which is why at the moment of the jump the angle of the hour hand to the y-axis is the same as the angle of the minute hand to the x-axis), but angleDifferenceH doesn't need to make the jump. Change this:

float angleDifferenceH = angleDifferenceM * (1.0/12.0); 

to

float angleDifferenceH = arcTanHour + angleDifferenceM * (1.0/12.0);

with an appropriate starting value for arcTanHour.

user2320861
  • 1,391
  • 2
  • 11
  • 28
  • Hmmm it doesn't seem to be working. This happens now: https://www.youtube.com/watch?v=YLRkUzXSDtQ&feature=youtu.be. I've updated my code, too – Alexyuiop Aug 19 '14 at 05:28
  • Also I noticed that you said the min using the min hand reference angle for both hands, but I was doing that because they both start at same angle. Would this change still be necessary? – Alexyuiop Aug 19 '14 at 05:34
  • Sorry, I'm trying to figure this out. Is the pan gesture recognizer on the entire view or only on the minute hand? – user2320861 Aug 19 '14 at 17:56
  • It's only on the minute hand – Alexyuiop Aug 19 '14 at 23:00