0

I've got a view in my app with a meter. The meter has a needle that rotates so far depending on a set of booleans I pass to it. The problem is that I am having a lot of trouble just getting the needle to rotate properly.

needle.layer.anchorPoint = CGPointMake(0.85, 0.5); // Set point of rotation

if(isHelpful == true)
{
    // rotate only a little
    needle.transform = CGAffineTransformMakeRotation(7*M_PI/180);
}

if(isNeeded == true)
{
    // rotate 95 degrees
    needle.transform = CGAffineTransformMakeRotation(95*M_PI/180);
}

if(isCritical == true)
{
    // rotate 175 degrees
    needle.transform = CGAffineTransformMakeRotation(175*M_PI/180);
}

This makes the needle move rotate perfectly, but it also changes its position for some reason, moving all over my nib. I need the image to stay in the same location on my nib and rotate around a specific axis to a degree I determine.

The image is 145x36 and its axis point is at 127x18.

Tanoro
  • 871
  • 2
  • 10
  • 30

1 Answers1

0

Changing the anchorPoint of your view's layer affects the position necessarily. Brad Larson has an excellent explanation in response to this question.

Community
  • 1
  • 1
bosmacs
  • 7,341
  • 4
  • 31
  • 31
  • I may have been unclear with my explanation. You can see from the code above that regardless of which rotation being done, the anchorPoint should be the same. The strange thing is that while the anchorPoint is the same for all rotations, the position of the needle at runtime is completely different! If isCritical executes, the needle is in the perfect spot and rotates perfectly. If isNeeded executes, the needle rotates well, but is positioned too high and too far to the left. If isHelpful executes, the needle is even farther to the right. – Tanoro Aug 03 '11 at 19:59