How can I reliably rotate an image around a point?

I read the following from the One -step affine transform to rotate around a point? :

CGAffineTransform transform = CGAffineTransformMakeTranslation(x, y); transform = CGAffineTransformRotate(transform, a); transform = CGAffineTransformTranslate(transform,-x,-y); 

However, when I do this, images are converted all over the map and rarely across the screen.

I tried a little to create a system in which images will be placed; if you run the loop and put a few values, a spiral will appear. However, although I could ultimately unravel the connection between the rotation of the images and the points that I wanted them to rotate, I wanted to ask for a better solution for “I have an image here, I think this moment is its center, I want so that it rotates around its center in this value, but does not move otherwise. "

I am trying to make a modified port http://JonathansCorner.com/ancient-clock/ , and now I'm trying to put the hour hand. All attempts to make the song and dance higher, and translate it so that its center was in the desired center, failed.

As for the hands of this watch, I can say "I want the hands to be in the next turns", and were they placed correctly around the center?

- EDIT -

The most important part of this code, edited in an attempt to use layers in response to a comment, is this:

 UIImage *hourHandImage = [UIImage imageNamed:@"hour-hand.png"]; UIImageView *hourHandView = [[UIImageView alloc] initWithImage:hourHandImage]; float hourRotation = .5; hourHandImage = [UIImage imageNamed:@"hour-hand.png"]; hourHandView = [[UIImageView alloc] initWithImage:hourHandImage]; CGAffineTransform transform = CGAffineTransformMakeTranslation(centerX - 21, -centerY - 121); // transform = CGAffineTransformRotate(transform, hourRotation); // transform = CGAffineTransformTranslate(transform, 2 * centerX, 2 * centerY); hourHandView.transform = transform; hourHandView.layer.anchorPoint = CGPointMake(centerX, centerY); hourHandView.layer.affineTransform = CGAffineTransformRotate(transform, hourRotation); [self.view addSubview:hourHandView]; 

Thanks,

- EDIT -

Now something has gone; if i follow some instructions i have:

 CGAffineTransform transform = CGAffineTransformMakeTranslation(100 -21, 100 -121); transform = CGAffineTransformRotate(transform, hour / 12.0 * M_PI * 2.0); transform = CGAffineTransformTranslate(transform, -330, 330); hourHandView.transform = transform; 

I would like to work on outputting these numbers, but this correctly displays the hour of 9:00.

Thank you for your help!

+9
ios objective-c transformation affinetransform
source share
4 answers

This line:

 CGAffineTransform transform = CGAffineTransformMakeTranslation(x, y); 

moves the image x, y.

(Note: you will rotate around the control point, no matter what you choose)

this line:

 transform = CGAffineTransformRotate(transform, a); 

rotates it around a control point.

If your control point is in the upper left corner (default), it will rotate around the upper left corner.

You need to install the following:

 [self layer].anchorPoint = CGPointMake(0.5, 0.5); 

to make it rotate around the center of the layer.

+10
source share

Rotation is always performed around (0,0).

To translate the point you FIRST you need to translate the pivot point to (0,0) and then rotate it and then translate it back.

So this should be:

 // cx, cy is center of screen // move (cx,cy) to (0,0) CGAffineTransform transform = CGAffineTransformMakeTranslation(-cx, -cy); // roate around (0,0) transform = CGAffineTransformRotate(transform, angleRadians); // mov e (0,0) back to (cx,cy) transform = CGAffineTransformTranslate(transform,cx,cy); 
+11
source share
 hourImageView.layer.anchorPoint = CGPointMake(0.5f, 0.9f); CGFloat angle = hour / 12.0 * M_PI * 2.0; hourHand.transform = CGAffineTransformMakeRotation(angle); 
+7
source share

Swift 5 version from Alex reply

Rotation is always performed around (0,0).

To move around a point, you first need to translate the pivot point to (0,0), and then rotate it, and then translate it back.

So it should be:

 // centerX, centerY is center of screen // move (centerX,centerY) to (0,0) var myTransform = CGAffineTransform(translationX: -centerX, y: -centerY) // rotate around (0,0) myTransform = myTransform.concatenating(CGAffineTransform(rotationAngle: angleInRadians)) // move (0,0) back to (cx,cy) myTransform = myTransform.concatenating(CGAffineTransform(translationX: centerX, y: centerY)) myView.transform = myTransform 
+1
source share

All Articles