I am currently trying to rotate the hour and minute hands of a clock. It is one of the mini games I am developing for an application. This function below randomly sets the position of both the hour hand and the minute hand. Currently I am keeping track of the rotation which will be used to reset the hand back to the original position. The problem occurs after the first or couple rotations and the minute hand appears sometimes in random positions. What can I do to fix or improve this or increase accuracy. Another question I have is what should the origin of the hand UIImageViews (top left? middle?). All suggestions will be greatly appreciated.
// function used to randomly move hands of clock and set answers
-(void)normal_move_hands
{
int hourHandLocation = arc4random() % 12; // 12 unique locations for hour hand
int miniuteHandLocation = arc4random() % 4; // 4 unique locations for minute hand
if(hourHandLocation ==0)
{
hourHand.transform =CGAffineTransformMakeRotation(M_PI / 6); // 1
rotationBackHour = (M_PI * 11 /6);
hourCorrectAnswer = [NSString stringWithFormat:@"1"];
}
if(hourHandLocation ==1)
{
hourHand.transform =CGAffineTransformMakeRotation(M_PI / 3); // 2
rotationBackHour = (M_PI * 10/6);
hourCorrectAnswer = [NSString stringWithFormat:@"2"];
}
if(hourHandLocation ==2)
{
hourHand.transform =CGAffineTransformMakeRotation(M_PI / 2); // 3
rotationBackHour = (M_PI * 3/2);
hourCorrectAnswer = [NSString stringWithFormat:@"3"];
}
if(hourHandLocation ==3)
{
hourHand.transform =CGAffineTransformMakeRotation(M_PI *2 /3); // 4
rotationBackHour = (M_PI * 4/3);
hourCorrectAnswer = [NSString stringWithFormat:@"4"];
}
if(hourHandLocation ==4)
{
hourHand.transform =CGAffineTransformMakeRotation(M_PI * 5 / 6 ); // 5
rotationBackHour = (M_PI * 7/6);
hourCorrectAnswer = [NSString stringWithFormat:@"5"];
}
if(hourHandLocation ==5)
{
hourHand.transform =CGAffineTransformMakeRotation(M_PI); // 6
rotationBackHour = (M_PI);
hourCorrectAnswer = [NSString stringWithFormat:@"6"];
}
if(hourHandLocation ==6)
{
hourHand.transform =CGAffineTransformMakeRotation(M_PI * 7 / 6); // 7
rotationBackHour = (M_PI * 5/6);
hourCorrectAnswer = [NSString stringWithFormat:@"7"];
}
if(hourHandLocation ==7)
{
hourHand.transform =CGAffineTransformMakeRotation(M_PI * 4 /3); // 8
rotationBackHour = (M_PI * 2/3);
hourCorrectAnswer = [NSString stringWithFormat:@"8"];
}
if(hourHandLocation ==8)
{
hourHand.transform =CGAffineTransformMakeRotation(M_PI * 3 / 2); // 9
rotationBackHour = (M_PI /2);
hourCorrectAnswer = [NSString stringWithFormat:@"9"];
}
if(hourHandLocation ==9)
{
hourHand.transform =CGAffineTransformMakeRotation(M_PI * 10 /6); // 10
rotationBackHour = (M_PI /3);
hourCorrectAnswer = [NSString stringWithFormat:@"10"];
}
if(hourHandLocation ==10)
{
hourHand.transform =CGAffineTransformMakeRotation(M_PI * 11 /6); // 11
rotationBackHour = (M_PI /6);
hourCorrectAnswer = [NSString stringWithFormat:@"11"];
}
if(hourHandLocation ==11) // 12
{
rotationBackHour = 0;
hourCorrectAnswer = [NSString stringWithFormat:@"12"];
}
if(miniuteHandLocation == 0) // 0 miniute
{
rotationBackMinute = 0;
minuteCorrectAnswer = [NSString stringWithFormat:@"0"];
}
if(miniuteHandLocation == 1) // 15 miniute
{
minuteHand.transform = CGAffineTransformMakeRotation(M_PI /2);
minuteCorrectAnswer = [NSString stringWithFormat:@"1"];
rotationBackMinute = (M_PI * 3/2);
}
if(miniuteHandLocation == 2) // 30 miniute
{
minuteHand.transform = CGAffineTransformMakeRotation(M_PI);
minuteCorrectAnswer = [NSString stringWithFormat:@"2"];
rotationBackMinute = (M_PI);
}
if(miniuteHandLocation == 3) // 45 miniute
{
minuteHand.transform = CGAffineTransformMakeRotation(M_PI * 3 /2);
minuteCorrectAnswer = [NSString stringWithFormat:@"3"];
rotationBackMinute = (M_PI /2);
}
}
This here is my reset function, which is used to reset the hands back to original position.
-(void)reset_hands
{
hourHand.transform = CGAffineTransformMakeRotation(rotationBackHour);
minuteHand.transform = CGAffineTransformMakeRotation(rotationBackMinute);
}