如何将以下动画转换为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)

它只显示一个圆圈,而不是动画。

最佳答案

您应该将键transform.scale与标量数字值一起使用,或者将transform与矩阵值一起使用。有关可用键的完整列表,请参见Key Value Coding Extensions in Core Animation Programming Guide

您可以使用以下方法创建矩阵值:

NSValue(caTransform3D:CATransform3DMakeScale(10.0, 10.0, 1.0))

关于ios - NSValue valueWithCATransform3D:在Swift3中,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/40298678/

10-12 05:23