NSValue valueWithCATransform3D:在Swift3中

时间:2016-10-28 05:35:37

标签: ios swift swift3 caanimation

如何将以下动画翻译成Swift3?

CABasicAnimation *scaleAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
scaleAnimation.fromValue = [NSValue valueWithCATransform3D:CATransform3DIdentity];
scaleAnimation.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeScale(2.5, 2.5, 1)];

我目前拥有的是

let circleEnlargeAnimation = CABasicAnimation(keyPath: "transform.scale")
circleEnlargeAnimation.fromValue = CATransform3DIdentity
circleEnlargeAnimation.toValue = CATransform3DMakeScale(10.0, 10.0, 1.0)

但我的动画无效......以下是完整的动画代码:

    let shapeLayer = CAShapeLayer()
    let center = CGPoint(x: sampleButton.bounds.width/2.0, y: sampleButton.bounds.height/2.0)
    let radius = CGFloat(10.0)

    let path = UIBezierPath(arcCenter: center, radius: radius, startAngle: 0.0, endAngle: (CGFloat(360.0 * M_PI) / 180.0), clockwise: true)
    shapeLayer.path = path.cgPath
    shapeLayer.fillColor = UIColor.white.cgColor
    shapeLayer.strokeColor = UIColor.white.cgColor
    shapeLayer.lineCap = kCALineCapRound

    shapeLayer.frame = sampleButton.bounds
    let circleEnlargeAnimation = CABasicAnimation(keyPath: "transform.scale")
    circleEnlargeAnimation.fromValue = CATransform3DIdentity
    circleEnlargeAnimation.toValue = CATransform3DMakeScale(10.0, 10.0, 1.0)
    circleEnlargeAnimation.duration = 1.0
    shapeLayer.add(circleEnlargeAnimation, forKey: nil)

    sampleButton.layer.addSublayer(shapeLayer)

它只显示一个圆圈,但不是动画......

1 个答案:

答案 0 :(得分:3)

您应该使用带有标量数值的键string或带有矩阵值的transform.scale。有关可用密钥的完整列表,请参阅Key Value Coding Extensions in Core Animation Programming Guide

您可以创建一个矩阵值,例如:

transform