简体   繁体   English

为什么 cornerRadius 的最大值不是矩形的一半? swift中用UIBezierPath绘制roundedRect

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

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?正如文档所说,cornerRadius 不能大于一半宽度,但在我的代码中它只有宽度的 1/3 rect 变成了一个圆,不知道为什么? need help需要帮忙

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

cornerRadius: The radius of each corner oval. cornerRadius:每个角椭圆的半径。 A value of 0 results in a rectangle without rounded corners.值为 0 会产生没有圆角的矩形。 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()

截屏

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. PS:没有通过运行这段代码来检查让我知道这里是否有任何问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM