用分针旋转时钟的时针

时间:2014-07-24 13:38:15

标签: ios ios7 core-animation

我正在使用CALayers制作模拟时钟。实时时钟工作完美,所有的手都像iOS中的默认时钟应用程序图标一样平稳移动。但是,当我尝试通过触摸/拖动来移动时钟指针时,分针完全移动但是时针没有移动。我试图移动时钟指针,就像它们在真正的基于齿轮的时钟中移动一样。关于如何计算时针相对于分针顺时针和逆时针运动的角度的任何解决方案?实际上我想要这个应用程序有点类似的功能https://itunes.apple.com/us/app/bb-teaching-clock/id612261763?mt=8

以下是分针旋转的代码

    float dx = touchPoint.x - minHand.position.x;
    float dy = touchPoint.y - minHand.position.y;

    deltaAngle = atan2f(dy,dx);
    startTransform = minHand.transform;
    float angleDifference;

    float dx = pt.x  - minHand.position.x;
    float dy = pt.y  - minHand.position.y;
    float ang = atan2f(dy,dx);

angleDifference = deltaAngle - ang;

    minHand.transform = CATransform3DRotate(startTransform, -angleDifference,0 ,0 ,1);
    NSLog(@"angleDifference: %f", angleDifference);

   CGFloat hourAngle = (1/12)*(angleDifference);
// hour hand should move in the same direction as minute hand but at a much slower rate
hourHand.transform = CATransform3DRotate (hourHand.transform, -hourAngle, 0, 0, 1);

1 个答案:

答案 0 :(得分:0)

尝试进行以下更改,其中centerPoint是时钟的中心......

float dx = touchPoint.x - centerPoint.x;
float dy = touchPoint.y - centerPoint.y;
deltaAngle = atan2f(dy,dx);

startTransform = minHand.transform;
float angleDifference;

dx = minHand.position.x - centerPoint.x;
dy = minHand.position.y - centerPoint.y;
float ang = atan2f(dy,dx);

angleDifference = ang - deltaAngle;