Created
March 16, 2021 01:36
-
-
Save coffeeexistence/0d7fc5dde5eca92b6c9dcc13848fdaab to your computer and use it in GitHub Desktop.
Animatable capsule/rounded rect path
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func makeCapsuleBorderPath(_ size: CGSize) -> CGPath { | |
// Have to create the path manually because "UIBezierPath(roundedRect:" creates a path | |
// that appears visually correct, but is actually uneven. This is only apparent when | |
// animating strokeStart or strokeEnd on a CAShapeLayer. | |
let path = UIBezierPath() | |
let centerY = size.height / 2 | |
let centerX = size.width / 2 | |
let cornerRadius = size.height / 2 | |
let topY: CGFloat = 0 | |
let bottomY = size.height | |
let leftCornerX = cornerRadius | |
let rightCornerX = size.width - cornerRadius | |
let leftArcCenterPoint = CGPoint(x: leftCornerX, y: centerY) | |
let rightArcCenterPoint = CGPoint(x: rightCornerX, y: centerY) | |
let centerTopPoint = CGPoint(x: centerX, y: topY) | |
path.move(to: centerTopPoint) | |
path.addLine(to: CGPoint(x: rightCornerX, y: topY)) | |
path.addArc( | |
withCenter: rightArcCenterPoint, | |
radius: centerY, | |
startAngle: deg2rad(-90), | |
endAngle: deg2rad(90), | |
clockwise: true | |
) | |
path.addLine(to: CGPoint(x: leftCornerX, y: bottomY)) | |
path.addArc( | |
withCenter: leftArcCenterPoint, | |
radius: centerY, | |
startAngle: deg2rad(90), | |
endAngle: deg2rad(-90), | |
clockwise: true | |
) | |
path.addLine(to: centerTopPoint) | |
path.close() | |
return path.cgPath | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hello, thanks for info about "UIBezierPath(roundedRect:", but your path don't use cornerRadius as it should be, if you create for example a rectangle with width: 120, height: 40 and set cornerRadius = 8, you didn't have this cornerRadius, your code create rectangle with cornerRadius = 20, here is how create rectangle with correct cornerRadius