CALayer - CABasicAnimation不围绕center/anchorPoint进行缩放

No *_*ing 24 core-animation ios

使用下面的代码我一直试图让我的CALayer围绕它的中心进行缩放 - 但它按左/上(0,0)进行缩放.我看到了这个问题:CALayer中的Anchor Point并尝试设置anchorPoint - 但在我设置之前它是[.5,.5]并且设置没有区别.也试过设置contentsGravity.

设置是我有一个CAShapeLayer添加到self.view.layer我做一些绘图,然后添加CABasicAnimation.动画运行正常 - 但它向上/左侧缩放 - 而不是视图的中心.

几个小时一直在修补它 - 它必须是简单的东西,但我没有得到它.

shapeLayer.anchorPoint = CGPointMake(.5,.5);
shapeLayer.contentsGravity = @"center";

printf("anchorPoint %f  %f\n", shapeLayer.anchorPoint.x, shapeLayer.anchorPoint.y);

CABasicAnimation *animation =
[CABasicAnimation animationWithKeyPath:@"transform.scale"];

animation.fromValue = [NSValue valueWithCATransform3D:CATransform3DIdentity];
animation.toValue =   [NSValue valueWithCATransform3D:CATransform3DMakeScale(0.1, 0.1, 1.0)];

[animation setDuration:1.0];
animation.fillMode=kCAFillModeForwards;
animation.removedOnCompletion=NO;
[shapeLayer addAnimation:animation forKey:@"zoom"];
Run Code Online (Sandbox Code Playgroud)

Kur*_*vis 50

什么是bounds你的层?

我打赌它没有大小; 尝试将其大小设置为与形状相同的大小.

CAShapeLayer绘制形状,种类覆盖的,独立的contents(与boundscontentsGravity等),你会在一个普通的使用CALayer.这可能有点令人困惑.


Dav*_*e G 5

还要确保在创建视图后添加动画如果您尝试在 viewDidLoad 中添加动画,它可能不会起作用。尝试将其添加到 viewDidAppear。

我只知道 Swift,但这里有一个例子:

override func viewDidAppear (animated: Bool) {
    addPulsation(yourButton)
}

func addPulsation (button: UIButton) {
    let scaleAnimation:CABasicAnimation = CABasicAnimation(keyPath: "transform.scale")
    scaleAnimation.duration = 1.0
    scaleAnimation.repeatCount = 100
    scaleAnimation.autoreverses = true
    scaleAnimation.fromValue = 1.05;
    scaleAnimation.toValue = 0.95;
    button.layer.addAnimation(scaleAnimation, forKey: "scale")
}
Run Code Online (Sandbox Code Playgroud)