iOS核心动画:旋转的锚点不正确

ant*_*kes 5 core-animation uiview ios

我想在iOS中实现基本旋转动画,其中视图围绕其中心点连续旋转.

但是,由于某种原因,旋转的锚点始终是父视图的原点,而不是旋转视图的中心.
因此,即使我手动设置锚点,视图也会围绕屏幕的左上角旋转.

这是我正在做的事情:

// Add shape layer to view
CAShapeLayer *shapeLayer = [[CAShapeLayer alloc] init];
CGRect shapeRect = CGRectMake(0, 0, 100, 100);
UIBezierPath *roundedRect = [UIBezierPath bezierPathWithRoundedRect:shapeRect cornerRadius:5];
shapeLayer.path = roundedRect.CGPath;
shapeLayer.anchorPoint = CGPointMake(0.5, 0.5);
shapeLayer.fillColor = [[UIColor redColor] CGColor];

[self.view.layer addSublayer:shapeLayer];

// Set rotation animation
CATransform3D rotationTransform = CATransform3DMakeRotation(M_PI, 0, 0, 1);
CABasicAnimation *rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform"];
rotationAnimation.toValue = [NSValue valueWithCATransform3D:rotationTransform];
rotationAnimation.duration = 1.0f;
rotationAnimation.cumulative = YES;
rotationAnimation.repeatCount = HUGE_VALF;

[shapeLayer addAnimation:rotationAnimation forKey:@"transform"];
Run Code Online (Sandbox Code Playgroud)

任何帮助,将不胜感激.

deb*_*k63 15

路径不定义图层的边界.所以,你的图层有CGRectZero界限.您的路径始于CGPointZero相对于边界.该层围绕其中心旋转,这也是CGPointZero......

所以,我看到了几个选择:

layer.path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(-50, -50,
                                                                100, 100)
                                        cornerRadius:5].CGPath;
Run Code Online (Sandbox Code Playgroud)

要么:

layer.bounds = CGPathGetBoundingBox(layer.path);
Run Code Online (Sandbox Code Playgroud)