Reputation: 418
I added a CALayer
in a UIView
.
And it has a fillColor.
It has also some width.
I want that layer to expand it's width from zero to actual width with CABasicAnimation
from left to right direction.
How to do that?
Upvotes: 3
Views: 8982
Reputation: 540
Answer for Swift 3. To make the layer change its width from left (not from center!) to right I used the following approach:
I have layer named "layer" and I want to change it width from 0 to 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")
Upvotes: 1
Reputation: 1235
Try animating your layer's bounds :
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")
It's working for me. Just don't forget to set the achor point.
Upvotes: 6