我目前正在尝试旋转时钟的时针和分针。这是我正在为应用程序开发的迷你游戏之一。下面的这个函数随机设置时针和分针的位置。目前,我正在跟踪旋转,该旋转将用于将手重置回原始位置。问题发生在第一次或几次旋转之后,分针有时会出现在随机位置。我能做些什么来修复或改进此问题或提高准确性。我的另一个问题是,手的起源应该是什么 UIImageViews(左上?中?所有建议将不胜感激。
// 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);
}
}
这是我的重置功能,用于将指针重置回原始位置。
-(void)reset_hands
{
hourHand.transform = CGAffineTransformMakeRotation(rotationBackHour);
minuteHand.transform = CGAffineTransformMakeRotation(rotationBackMinute);
}
从我上面的评论中: 您可以简单地重置转换
hourHand.transform = CGAffineTransformMakeRotation(0);
// or:
hourHand.transform = CGAffineTransformIdentity;
而且没有必要记住rotationBackHour
.(设置transform
不会累积。
另请注意,您可以将代码简化为
hourHand.transform = CGAffineTransformMakeRotation((hourHandLocation + 1)*M_PI/6.0);
hourCorrectAnswer = [NSString stringWithFormat:@"%d", hourHandLocation + 1];
使许多 if 块过时。
"有时出现在随机位置"问题是由自动布局或自动调整大小选项引起的。关闭它们或修复它们,以使小时/分钟视图与时钟的大小和相对位置不会改变。