0

我一直在尝试制作一个时针跟随分针的模拟时钟。尝试了所有我能想到的,但比例不对。

拖动分针可以正常工作,所以我认为这是我计算时针应该旋转的比率的方式。

这是我最近尝试的许多...

代码:

#define DEGREES_TO_RADIANS(angle) ((angle) / 180.0 * M_PI)
#define RADIANS_TO_DEGREES(radians) ((radians) * (180.0 / M_PI))

CGFloat oldAngleInRadians = M_PI_2;

- (void)rotateNode:(SKSpriteNode *)node forTouch:(UITouch *)touch
{
    if ([node isEqual:dial]) return;

        CGPoint positionInDial = [touch locationInNode:dial];
        float deltaY = positionInDial.y - minuteIndicator.anchorPoint.y;
        float deltaX = positionInDial.x - minuteIndicator.anchorPoint.x;
        CGFloat angleInRadians = atan2f(deltaY, deltaX);

        // Minute hand rotation in this case
        [node runAction:[SKAction rotateToAngle:angleInRadians - (M_PI / 2) duration:0]];

    if (self.difficulty == kMediumDifficulty ) {
        [self updateHourhandAngleWithNewAngle:angleInRadians oldAngle:oldAngleInRadians];

        oldAngleInRadians = angleInRadians;
    }
}

#pragma mark - Rotate hour hand with minute hand

- (void)updateHourhandAngleWithNewAngle:(CGFloat)newAngle oldAngle:(CGFloat)oldAngle
{
    double newAngleDeg = RADIANS_TO_DEGREES(newAngle);
    double oldAngleDeg = RADIANS_TO_DEGREES(oldAngle);

    double differenceDeg = 0;

    if (newAngleDeg > 0 && oldAngleDeg < 0) {
        differenceDeg = oldAngleDeg - ( - 1 * newAngleDeg );
    } else if (newAngleDeg < 0 && oldAngleDeg > 0) {
        differenceDeg = fabsf(newAngleDeg) - oldAngleDeg;
    } else {
        differenceDeg = newAngleDeg - oldAngleDeg;
    }

    [hourIndicator runAction:[SKAction rotateByAngle:DEGREES_TO_RADIANS((differenceDeg / 12.0f)) duration:0]];
}
4

1 回答 1

1

不确定我是否正确理解了您的问题,但这就是我使用 SpriteKit 制作模拟时钟的方式:

// MyScene.m
- (void) didMoveToView:(SKView *)view
{
    [self.clock didEnterScene];
}

- (void) update:(CFTimeInterval)currentTime
{
    CGFloat dt = 0.0;
    if (self.lastUpdateInterval > 0)
    {
        dt = currentTime - self.lastUpdateInterval;
    }
    [self.clock update:dt];
    self.lastUpdateInterval = currentTime;
}

// ClockNode.m
- (void) didEnterScene
{
    SKSpriteNode* secHand = [SKSpriteNode spriteNodeWithColor:[UIColor blueColor] size:CGSizeMake(2, 100)];
    SKSpriteNode* minHand = [SKSpriteNode spriteNodeWithColor:[UIColor greenColor] size:CGSizeMake(5, 100)];
    SKSpriteNode* hourHand = [SKSpriteNode spriteNodeWithColor:[UIColor redColor] size:CGSizeMake(10, 70)];

    secHand.position = CGPointMake(CGRectGetMidX(self.scene.frame), CGRectGetMidY(self.scene.frame));
    minHand.position = secHand.position;
    hourHand.position = secHand.position;

    secHand.anchorPoint = CGPointMake(0.5, 1.0);
    minHand.anchorPoint = CGPointMake(0.5, 1.0);
    hourHand.anchorPoint = CGPointMake(0.5, 1.0);

    [self addChild:secHand];
    [self addChild:minHand];
    [self addChild:hourHand];

    _secHand = secHand;
    _minHand = minHand;
    _hourHand = hourHand;

    _secHand.zRotation = M_PI;
    _minHand.zRotation = M_PI;
    _hourHand.zRotation = M_PI;

    _msec = 0.0f;
    _sec = 0.0f;
    _min = 0.0f;
    _hour = 0.0f;
}

- (void) update:(CGFloat)dt
{
    _msec += dt;
    if (_msec >= 1)
    {
        _msec -= 1;
        _sec += 1;
        _secHand.zRotation -= 2*M_PI / 60;
    }
    if (_sec >= 60)
    {
        _sec -= 60;
        _min += 1;
        _minHand.zRotation -= 2*M_PI / 60;
    }
    if (_min >= 60)
    {
        _min -= 60;
        _hour += 1;
        _hourHand.zRotation -= 2*M_PI / 12;
    }
    if (_hour >= 12)
    {
        _hour -= 12;
    }
}

模拟时钟

于 2014-03-23T09:27:25.440 回答