Why the max value of cornerRadius is not half of the rectangle? Drawing roundedRect with UIBezierPath in swift

373 Views Asked by At

as the document says the cornerRadius can not larger than half width, but in my code it only 1/3 of the width the round rect become a circle, can't know why? need help

document: https://developer.apple.com/documentation/uikit/uibezierpath/1624356-init

cornerRadius: The radius of each corner oval. A value of 0 results in a rectangle without rounded corners. Values larger than half the rectangle’s width or height are clamped appropriately to half the width or height.

var path = UIBezierPath(roundedRect: CGRect(x: 50, y: 200, width: 94, height: 94), cornerRadius: 31)
path.stroke()

path = UIBezierPath(roundedRect: CGRect(x: 50, y: 300, width: 94, height: 94), cornerRadius: 30.5)
path.stroke()
                
path = UIBezierPath(roundedRect: CGRect(x: 50, y: 400, width: 94, height: 94), cornerRadius: 31.5)
path.stroke()

screenshot

1

There are 1 best solutions below

0
Muneeb Awan On

I think it is because of a type casting error.

Try this.

let xCor: CGFloat = 50.0
let width: CGFloat = 94.0
let height: CGFloat = 94.0

let cornerRadius: CGFloat = width / 3.0

var path = UIBezierPath(roundedRect: CGRect(x: xCor, y: 200.0, width: width, height: height), cornerRadius: cornerRadius)
path.stroke()

path = UIBezierPath(roundedRect: CGRect(x: xCor, y: 300.0, width: width, height: height), cornerRadius: cornerRadius)
path.stroke()
                
path = UIBezierPath(roundedRect: CGRect(x: xCor, y: 400.0, width: width, height: height), cornerRadius: cornerRadius)
path.stroke()

PS: didnt check by running this code let me know if there is any problem here.