Core Animation, a rotating layer around an arbitrary point

How can I rotate a layer using Core Animation around an arbitrary point? (In my case, a point that is not inside the layer that I want to rotate)

I prefer to do this without changing the anchor point, because if I don't get something wrong every time I change the anchor point it also changes the position of the layer.

I tried something like this, but that didn't work:

[UIImageView beginAnimations:nil context:nil];
CATransform3D rotationTransform = CATransform3DIdentity;
rotationTransform = CATransform3DTranslate(rotationTransform, 0.0, -100.0, 0.0);
rotationTransform = CATransform3DRotate(rotationTransform, DegreesToRadians(180),
                        0.0, 0.0, 1.0);
rotationTransform = CATransform3DTranslate(rotationTransform, 0.0, 100.0, 0.0)
shape1.layer.transform = rotationTransform;
[UIImageView commitAnimations];

It appears that the rotation axis rotates during rotation.

+3
source share
4 answers

I finally did this by creating a new, larger layer centered on my axis of rotation and setting the layer that I want to rotate as a sublayer.

Then I rotate the larger layer instead of the underlayer

0

, " " - .

, , , , anchorPoint superlayer .

, , - , - .

!

+3

, :

  • (-rotCenterX, rotCenterY)
  • (rotCenterX, rotCenterY)
+2

Your initial decision is not so good, as the translation is animated in the same way as the rotation. Thus, the "custom center" of your rotation moves during the rotation. What you really want is a non-animation translation and rotation with animation based on this non-animated translation. The fact is that I currently have the same problem, and I could not come up with a solution. I'm looking at CATransaction for now, hoping this might be the key ...

+1
source

Source: https://habr.com/ru/post/1698234/


All Articles