iOS:如何围绕中心点旋转UIImageView?

时间:2013-11-10 13:16:59

标签: ios css objective-c uiimageview

我正在尝试旋转时钟的时针和分针。这是我正在为应用程序开发的迷你游戏之一。下面的这个功能随机设置时针和分针的位置。目前我正在跟踪将用于将手重置回原始位置的旋转。在第一次或几次旋转之后出现问题,并且分针有时出现在随机位置。我该怎么做才能修复或改善这一点或提高准确性。我的另一个问题是手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);
}

1 个答案:

答案 0 :(得分:1)

从我的上述评论中:您只需使用

重置转换即可
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-blocks过时。

“有时出现在随机位置”问题是由Autolayout或Autosizing选项引起的。关闭它们或修复它们,使小时/分钟视图的大小和相对位置不会改变。