24小时模拟时钟

我试图做一个24小时的模拟时钟,目前有一个12小时的时钟。

我写的是:

currentTime = [NSDate date]; dateComponents = [calendar components:unitFlags fromDate:currentTime]; hour = [dateComponents hour]; minute = [dateComponents minute]; second = [dateComponents second]; hourAngle = (30 * hour + minute / 2); minAngle = (6 * minute); secAngle = (6 * second); secondHand.transform = CGAffineTransformMakeRotation(secAngle * M_PI / 180); minuteHand.transform = CGAffineTransformMakeRotation(minAngle * M_PI / 180); hourHand.transform = CGAffineTransformMakeRotation(hourAngle * M_PI / 180); 

这对于一个12小时的时钟是完美的,但是我想把它做成24,所以我build议把我的angular度值除以2:

 hourAngle = (30 * hour + minute / 2) /2; minAngle = (6 * minute) /2; secAngle = (6 * second) /2; 

然而,这给了我一个180度的顶部到底部angular度,这个angular度在达到180度后重置。

我需要什么来改变我的时钟360度全方位的旋转?

显然,分秒的angular度不会改变。 只有小时angular度减半。 此外,请确保小时达到所有值高达23。

你想要做的是把24小时的时钟包装到12小时的旋转,你可以使用这样的模数:

 hourAngle = (30 * (hour % 12) + minute / 2); 

我创build了一个项目,使模拟风格的时钟在这里PSAnalogClock 。 我用这个来计算时间

编辑 – 感谢@ nes1983

 int hoursFor12HourClock = hours % 12; 

然后计算我使用的angular度

 float rotationForHoursComponent = hoursFor12HourClock * degreesPerHour; float rotationForMinuteComponent = degreesPerMinute * [self minutes]; float totalRotation = rotationForHoursComponent + rotationForMinuteComponent; 

不是直接的答案,更像是一个额外的视angular。 ;)下面的一段代码是从应用程序弹性时钟获取的 ,该时钟在AppStore上可用,并由您的无头独立编程器编写。

没有太多的解释,也许它给你一些额外的洞察力,看看cocos2d步骤方法ii为弹性时钟写的永恒:

 //You remember the math... //reference: http://en.wikipedia.org/wiki/Clock_angle_problem - (void)step:(ccTime)delta { //DLog(@"STEP delta: %f", delta); NSDate *d = [NSDate date]; //NSTimeInterval interval = [d timeIntervalSince1970]; NSTimeInterval interval = [d timeIntervalSince1970] + ([[NSTimeZone localTimeZone] secondsFromGMT]); //DLog(@"date=%@ interval=%f", d, interval); float sixty = 60 * h13Ratio; //change 60 minutes to 60*k minutes //normal clock int days = interval / 86400; //24*60*60 h = (int)interval / 3600 - (days * 24); //1 hour = 60*60 m = (interval/60) - (days * 24 * 60) - (h * 60); s = (int)interval % 60; //elastic clock //int _days = days * h13Ratio; not used _h = h * h13Ratio; _m = m * h13Ratio; _s = s * h13Ratio; //DLog(@"hours=%i minutes=%i seconds=%i", _hours, _minutes, _seconds); //angle speed for clock hands float h_speed = 360 / (h13Number * sixty); float m_speed = (360 / sixty); float s_speed = (360 / sixty); //rotation angles for clock hands h_roto = (h_speed * _h * sixty) + (h_speed * _m); m_roto = m_speed * _m; s_roto = s_speed * _s; //DLog(@"h_roto=%f m_roto=%f s_roto=%f", h_roto, m_roto, s_roto); //rotate clock hands //[self updateClockHands]; } 

如果有人将h13Ratiovariables设置为这样的值:

  h13Number = numberOfHoursInTheDay_OnTheClockFace; //24 if You want a 24h day, or 13 if You want a 13h day h13Ratio = h13Number*2 / 24.0f; 

h13Ratio会是2 。 这是有道理的,因为钟面上有两倍的数字。 🙂

请享用