I'm attempting to make a 24 hour analog clock and currently have a 12 hour clock.
What I wrote:
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);
This works perfectly for a 12 hour clock but I would like to make it 24 so I was advised to divide my angle values by 2 like this:
hourAngle = (30 * hour + minute / 2) /2;
minAngle = (6 * minute) /2开发者_JAVA技巧;
secAngle = (6 * second) /2;
However this gives me a top to bottom 180 degree angle which resets after reaching the 180 degrees.
What do I need to change to get a full 360 degree rotation for my clock?
Clearly, the angles for minutes and seconds don't change. Only the hour angle is halved. Also, be sure that hour reaches all values up to 23.
What you want to do is to wrap the 24 hour clock to 12 hour rotations, you can use a modulo for this like so:
hourAngle = (30 * (hour % 12) + minute / 2);
I created a project that makes an analog style clock which is available here PSAnalogClock. I used this to work out the hours
EDIT - thanks to @nes1983
int hoursFor12HourClock = hours % 12;
Then to calculate the angle I used
float rotationForHoursComponent = hoursFor12HourClock * degreesPerHour;
float rotationForMinuteComponent = degreesPerMinute * [self minutes];
float totalRotation = rotationForHoursComponent + rotationForMinuteComponent;
not a direct answer, more like an additional perspective. ;) the following piece of code is taken from the app elastic time clock, which is available on the AppStore and was written by Your Headless Standup Programmer.
without much explaining, maybe it gives You some extra insight into timelessness taking a look at the cocos2d step method ii wrote for elastic time clock:
//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];
}
if one would have set the h13Ratio variable to have a value like this:
h13Number = numberOfHoursInTheDay_OnTheClockFace; //24 if You want a 24h day, or 13 if You want a 13h day
h13Ratio = h13Number*2 / 24.0f;
the h13Ratio would be 2. which makes sense, since there is twice as much numbers on the clock face. :)
enjoy
精彩评论