NSValue valueWithCATransform3D:在 Swift3 中
Posted
技术标签:
【中文标题】NSValue valueWithCATransform3D:在 Swift3 中【英文标题】:NSValue valueWithCATransform3D: in Swift3 【发布时间】:2016-10-28 05:35:37 【问题描述】:如何将以下动画翻译成 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】:您应该将键 transform.scale
与标量数值或 transform
与矩阵值一起使用。有关可用密钥的完整列表,请参阅 Key Value Coding Extensions in Core Animation Programming Guide。
您可以创建一个矩阵值,例如:
NSValue(caTransform3D:CATransform3DMakeScale(10.0, 10.0, 1.0))
【讨论】:
以上是关于NSValue valueWithCATransform3D:在 Swift3 中的主要内容,如果未能解决你的问题,请参考以下文章