[英]How to reduce the width of UIButton from both left and right with animation?
[英]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.