首页 > 解决方案 > 为什么cornerRadius的最大值不是矩形的一半?用 UIBezierPath 快速绘制roundedRect

问题描述

正如文件所说,cornerRadius 不能大于一半宽度,但在我的代码中,圆形矩形只有宽度的 1/3 变成了圆形,不知道为什么?需要帮忙

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

cornerRadius:每个角椭圆的半径。值为 0 会生成一个没有圆角的矩形。大于矩形宽度或高度一半的值被适当地限制为宽度或高度的一半。

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()

截屏

标签: swiftcore-graphicsquartz-2d

解决方案


我认为这是因为类型转换错误。

尝试这个。

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


推荐阅读