I am trying to animate layer path property using CABasicAnimation
key path But. not getting exact results
What change of path animation i am looking for is below
Expected Animation
Note:- Please ignore switch.. my focus is just path change from circular
to moon
animation
What i tried
Here is my code that i have tried to get this path conversion animation
@IBDesignable class CustomView: UIView {
private lazy var shapeLayer = CAShapeLayer()
override init(frame: CGRect) {
super.init(frame: frame)
commonInit()
}
required init?(coder: NSCoder) {
super.init(coder: coder)
commonInit()
}
private func commonInit() {
shapeLayer.frame = bounds
shapeLayer.fillColor = UIColor.black.cgColor
shapeLayer.strokeColor = UIColor.red.cgColor
shapeLayer.lineWidth = 3
layer.addSublayer(shapeLayer)
}
override func layoutSubviews() {
shapeLayer.path = drawSunShape(bounds)
}
private func drawSunShape(_ group:CGRect) -> CGPath {
let bezierPath = UIBezierPath()
let radius = group.maxX/2
let center = CGPoint(x: group.midX, y: group.midY)
bezierPath.addArc(withCenter:center, radius: radius, startAngle: -.pi/2, endAngle: 3 * .pi * 0.5 , clockwise: true)
return bezierPath.cgPath
}
private func drawMoonShape(_ group:CGRect) -> CGPath {
let rad : CGFloat = group.maxX/2
let center = CGPoint(x: group.midX, y: group.midY)
let big = UIBezierPath()
big.addArc(withCenter: center, radius: rad, startAngle:-.pi/2.0, endAngle: .pi/2.0, clockwise: true)
big.addArc(withCenter: CGPoint(x: rad * cos(.pi/2.0), y: center.y), radius: rad * sin(.pi/2.0), startAngle: .pi/2.0, endAngle: -.pi/2.0, clockwise: false)
big.close()
return big.cgPath
}
private func animateToOffShape() {
let anim = CABasicAnimation(keyPath: "path")
anim.toValue = drawMoonShape(bounds)
anim.duration = 5.0
shapeLayer.add(anim, forKey: nil)
}
}
What i am getting from this code
UPDATE
i am able to achieve this by the help of @sweeper and @max
but i want it from center right
Any pointers how i can achieve the expected result. Thanks