My button is being resized by using auto layout. The button has a CAShapeLayer which it's frame is being set at layoutSubviews
:
var initial = true
override func layoutSubviews() {
super.layoutSubviews()
if initial {
border.frame = layer.bounds
border.path = UIBezierPath(rect: bounds).CGPath
initial = false
}
}
Because of this, whenever the size of this button is changed, the layer is changed immediately without any animation (which hides the button animation itself).
I've tried to add an animation:
override func layoutSubviews() {
super.layoutSubviews()
func movePosition() {
let fromValue = border.position.x
let toValue = center.x
CATransaction.setDisableActions(true)
border.position.x = toValue
let positionAnimation = CABasicAnimation(keyPath: "position.x")
positionAnimation.fromValue = fromValue
positionAnimation.toValue = toValue
positionAnimation.duration = 0.5
border.addAnimation(positionAnimation, forKey: "position")
}
func changesWidth() {
let fromValue = border.frame.size.width
let toValue = layer.bounds.width
CATransaction.setDisableActions(true)
border.frame.size.width = toValue
let widthAnimation = CABasicAnimation(keyPath: "frame.size.width")
widthAnimation.fromValue = fromValue
widthAnimation.toValue = toValue
widthAnimation.duration = 0.5
border.addAnimation(widthAnimation, forKey: "width")
}
if initial {
border.frame = layer.bounds
border.path = UIBezierPath(rect: bounds).CGPath
initial = false
} else {
movePosition()
changesWidth()
}
}
But with this, the animation is totally messed up (and layoutSubview
is called a few times initially so it messes the layers up at the start.
Is it right to be trying to animate the layer in layoutSubviews
at all? Or should I calculate the widths manually and animate the layers from there?