24小时模拟时钟

时间:2011-08-17 12:08:15

标签: iphone objective-c ios

我正在尝试制作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小时,所以我建议将角度值除以2:

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

然而,这给了我一个从上到下的180度角,在达到180度后重置。

我需要更改什么才能让我的时钟完全旋转360度?

4 个答案:

答案 0 :(得分:4)

显然,分钟和秒的角度不会改变。只有小时角减半。此外,请确保小时达到最多23个值。

答案 1 :(得分:1)

你想要做的是将24小时制旋转到12小时轮换,你可以像这样使用模数:

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

答案 2 :(得分:0)

我创建了一个项目,它可以制作模拟样式时钟PSAnalogClock。我用它来计算时间

编辑 - 感谢@ nes1983

int hoursFor12HourClock = hours % 12;

然后计算我使用的角度

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

float totalRotation = rotationForHoursComponent + rotationForMinuteComponent;

答案 3 :(得分:0)

不是直接的答案,更像是一个额外的视角。 ;)以下代码段来自应用程序elastic time clock,该应用程序可在AppStore上获得,由您的Headless Standup Programmer编写。

没有太多解释,也许它会给你一些关于永恒性的额外见解,看看我为elastic time clock写的cocos2d步骤方法:

//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];    
}

如果有人将 h13Ratio 变量设置为具有如下值:

    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 。这是有道理的,因为钟面上有两倍的数字。 :)

享受