繁体   English   中英

如何从左到右更改带有动画的CALayer的宽度?

[英]How to change the width of a CALayer with animation from left to right?

我在UIView添加了CALayer 它具有fillColor。 它也有一些宽度。 我希望该层使用CABasicAnimation从左向右将其宽度从零扩展到实际宽度。

怎么做?

尝试设置图层边界的动画:

let a = CALayer()
a.bounds = CGRect(x: 0, y: 0, width: 100, height: 40)
a.position = CGPoint(x: 20, y: 20)
a.anchorPoint = CGPoint(x: 0, y: 0)
view.layer.addSublayer(a)

let anim = CABasicAnimation(keyPath: "bounds")
anim.duration = 2
anim.fromValue = NSValue(CGRect: CGRect(x: 0, y: 0, width: 0, height: 30))
anim.toValue = NSValue(CGRect: CGRect(x: 0, y: 0, width: 100, height: 30))
a.addAnimation(anim, forKey: "anim")

它为我工作。 只是别忘了设置起始点。

Swift 3的答案。要使图层的宽度从左侧(而不是从中心!)更改为右侧,我使用了以下方法:

我有一个名为“ layer”的图层,我想将其宽度从0更改为myWidth

 layer.position = CGPoint(x: 0, y: 0)
 layer.anchorPoint = CGPoint(x: 0, y: 0.5)
 let layerAnimation = CABasicAnimation(keyPath: "bounds.size.width")
 layerAnimation.duration = 2
 layerAnimation.fromValue = 0
 layerAnimation.toValue = myWidth
 layer.add(layerAnimation, forKey: "anim")

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM