Search code examples
iosswiftcore-animationcalayeruibezierpath

UIBezierPath animation from circle to rounded square


I'm trying to animate from a circle to a square with rounded corners. As far as I understand, number of points from both paths should be equal for smooth animation. So I wrote a function that builds a circle bases on a number of arcs:

private func circleShape(segmentNumber: Int = 8, radius: CGFloat = 32) -> CGPath {
    let center = self.bounds.center
    let circlePath = UIBezierPath()
    let segmentAngle = CGFloat.pi * CGFloat(2) / CGFloat(segmentNumber)
    var currentAngle: CGFloat = 0

    for _ in 0 ..< segmentNumber {
        let nextAngle = currentAngle + segmentAngle
        circlePath.addArc(withCenter: center, radius: radius,
                startAngle: currentAngle, endAngle: nextAngle, clockwise: true)
        currentAngle = nextAngle
    }

    circlePath.close()
    return circlePath.cgPath
}

I'm checking number of points with getPathElementsPointsAndTypes() and it increases unproportionally to the segmentNumber (segmentNumber: 1, points: 13; sN: 2, p: 14; sN: 3, p: 21; sN: 4, p: 16). So I can't find a segmentNumber that would give the same number of points (19) as the function that draws the square:

private func squareShape(size: CGFloat = 44, radius: CGFloat = 8) -> CGPath {
    let rect = CGRect(center: self.bounds.center, size: CGSize(width: size, height: size))
    let left = CGFloat.pi
    let up = CGFloat(1.5) * CGFloat.pi
    let down = CGFloat.pi / CGFloat(2)
    let right = CGFloat(0.0)
    let squarePath = UIBezierPath()

    squarePath.addArc(withCenter: CGPoint(x: rect.minX + radius, y: rect.minY + radius), radius: radius, startAngle: left, endAngle: up, clockwise: true)
    squarePath.addArc(withCenter: CGPoint(x: rect.maxX - radius, y: rect.minY + radius), radius: radius, startAngle: up, endAngle: right, clockwise: true)
    squarePath.addArc(withCenter: CGPoint(x: rect.maxX - radius, y: rect.maxY - radius), radius: radius, startAngle: right, endAngle: down, clockwise: true)
    squarePath.addArc(withCenter: CGPoint(x: rect.minX + radius, y: rect.maxY - radius), radius: radius, startAngle: down, endAngle: left, clockwise: true)
    squarePath.close()

    return squarePath.cgPath
}

Animation code:

let animation = CABasicAnimation(keyPath: "path")
animation.duration = 2
animation.toValue = self.circleShape()
animation.timingFunction = CAMediaTimingFunction(name: CAMediaTimingFunctionName.linear)

self.testLayer.add(animation, forKey: "pathAnimation")

Result:

enter image description here

What could I change to make the animation same smooth as in example without rounded rects?

enter image description here


Solution

  • You can simply have two paths

        func circlePath () -> UIBezierPath {
            return UIBezierPath(roundedRect: CGRect(x:0, y:0, width:50, height:50), cornerRadius: 25)
        }
    
        func squarePath () -> UIBezierPath {
            return UIBezierPath(roundedRect: CGRect(x:0, y:0, width:30, height:30), cornerRadius: 4)
        }