views:

41

answers:

2

So I have my clock as a UIImageView. Thats perfect the clock doesnt move. I am happy with that.

I am so have the hand of my clock as another UIImageView and it is placed on top of the clock ImageView.

I want to get my Hand of the clock to rotate 360degrees over 10 seconds, but the tricky part is that I want to make it rotate not on the origin at TopLeft of the clockhand ImageView put at a point 20,20 of the clockhand ImageView.

Here is my code so far, it rotates the clock hand 360degrees over 10seconds, but it rotates it around the topleft of the ImageView.

                CABasicAnimation *rotate;
            rotate = [CABasicAnimation 
animationWithKeyPath:@"transform.rotation"];
            rotate.fromValue = [NSNumber numberWithFloat:0];
            rotate.toValue = [NSNumber numberWithFloat:6.283185307];
            rotate.duration = 10.00;
            rotate.repeatCount = 1;
            needleImageView.layer.anchorPoint = CGPointMake(0.5, 0.5);
            [needleImageView.layer addAnimation:rotate forKey:@"10"];

Anybody able to gimme some hints about getting this to rotate around the point 20,20 of needleImageView ?

Thanks, -Code

A: 

You can rotate the superview or change the anchorPoint property of its root layer, see http://www.informit.com/blogs/blog.aspx?uk=Ask-Big-Nerd-Ranch-Rotating-an-iPhone-View-Around-a-Point

Wernight
A: 

By default the transform will be applied with respect to the center. You need to change the layer's anchorPoint to change it. Check link text for details information on it.

One important thing is if you change anchorPoint then the position of the view will also change, as the position property of layer is respect to the anchor point. So you need to set the old frame after changing anchotPoint.

You need to do something like this.

// your image is 100 x 100 pixels and you want to make (20, 20) as the transformation center
CGRect oldFrame = myView.frame;
myView.layer.anchorPoint = CGPointMake(0.2, 0.2);
myView.frame = oldFrame

BTW, I have not complied the code, just typed so there might be some typo.

taskinoor