Rotating an image around a pre-determined axis
I've got a view in my app with a meter. The m开发者_如何学Goeter 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.
Changing the anchorPoint
of your view's layer affects the position necessarily. Brad Larson has an excellent explanation in response to this question.
精彩评论